更新时间:2023-08-23 15:26:58
你可以直接在React中操作dom。这允许集成现有的JS库或React无法很好地处理的自定义需求。
You can manipulate the dom directly inside React. This permits to integrate existing JS libraries or for custom needs not handled well by React.
你可以在这里找到一个例子:
You can find an exemple here:
这就是它的样子:
React与像Isotope这样的库集成的问题是你最终会遇到2个不同的问题库试图更新相同的dom子树。由于React使用差异,它假设它是单独使用dom。
The problem with integration of React and a library like Isotope is that you will end up having 2 different libraries trying to update the same dom subtree. As React work with diffs, it kind of assumes that it is alone modyfing the dom.
所以想法可能是创建一个只渲染一次的React组件,永远不会自我更新。您可以通过以下方式确保:
So the idea could be to create a React component that will render only one time, and will never update itself. You can ensure this with:
shouldComponentUpdate: function() {
return false;
}
有了这个,你可以:
componentDidMount
,初始化由React挂载的dom节点上的同位素componentDidMount
, initialize isotope on the dom node mounted by React这就是全部。现在React永远不会再次更新dom的这一部分,并且Isotope可以***地操纵它而不会干扰React。
And that's all. Now React will never update this part of the dom again, and Isotope is free to manipulate it like it wants to without interfering with React.
此外,就我而言理解,Isotope并不打算与动态项目列表一起使用,因此有一个永不更新的React组件是有意义的。
In addition, as far as I understand, Isotope is not intented to be used with a dynamic list of items so it makes sense to have a React component that never updates.