The happy medium
I totally agree with Christian Blavier's opinion of not using client-side frameworks such as Backbone, AngularJS or EmberJS unless the situation strongly requires it. This libraries are sometimes very complex and as a Rails developer and lover I like to use it's good parts instead of just having an API application just serving json.
React, the V in MV*
- React focuses on entities called Components.
- Every Component has an immutable set of properties called props which are passed to the component as parameters and cannot be changed after the it's rendered.
- Every Component also has an internal state, which can be changed, and when it does, the components re-renders again.
- Every Component has a render function.
- Instead of using a template engine, components use plain JS or JSX to render.
- What makes React really powerful and fast is the way it renders components when their props or state change. Instead of manipulating the existing DOM as other frameworks do, it uses what they call the virtual DOM.
- When the state of a Component changes, Reactjs checks for diffs between the virtual DOM and the current DOM and only applies those changes instead of having to re-render the whole DOM again. So imagine the virtual DOM as a GIT system for the DOM.
- Components can be nested, so you can write really small reusable components which pass data from parents to children, and bind event handling from children to parents.
I really like React. After a couple of months writing components for my new pet project I feel very comfortable with it and I'm really happy with the final result. It's really easy to learn how it works and to use it in those views where there is some extra complex behavior needed, but not too much as for using a full MV* framework for the whole project. In the next post I will write a real example of how to integrate React with Rails and build some reusable components, so stay tuned :)
Happy coding!comments powered by Disqus