React.js and Isotope.js

I’m checking out React.js and trying to figure out how this library can work together with Isotope.js. The documentation of React says that it plays nicely with other libraries, but using it with library that changes DOM on its own seems like no sense of using React.

Can someone explain to me, how can I take advantage of React in my webapp that uses Isotope.js as layout ?

Answer

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:

https://github.com/stample/gulp-browserify-react-phonegap-starter/blob/master/src/js/home/homeComponents.jsx#L22

And here’s what it looks like:

image


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.

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; 
}

With this you can:

  • Use React to generate your isotope item html elements (you can also create them without React)
  • On componentDidMount, initialize isotope on the dom node mounted by 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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *