ReactJS

Luis Matute

ReactJS

ReactJS Bind() Hell

I (along with many others) hate having to call React's components methods with something that looks like: this.method.bind(this). If you don't know what I'm talking about, read more about it in this post. In my projects, I add to the React.Component class prototype, a helper method which helps me remove those crazy bindings. I do, at top level file, the following addition to the Component class: // Deps ========================================= import React from 'react'; ... // Adding _bind helper to Components ============ React.Component.prototype._bind = function _bind(...methods) { methods.forEach((method) => { this[method] = this[method].bind(this); }); }; Pretty neat, right…

Read More

Luis Matute

ReactJS jQuery

Communicate between components (ReactJS)

So I've been playing a lot with ReactJS lately and one of the problems I was facing was that I needed to comunicate from one component to another. They had different root components, so I couldn't pass the handlers between them. When the components can't communicate between any sort of parent-child relationship, the documentation recommends setting up a global event system. The solution I ended up using was a small snippet but rather elegant. I'm going to rely on JavaScript Events to create a small publish–subscribe pattern. If you have not used pub/sub before, its a messaging pattern…

Read More