Add Lifecycle Hooks to a Functional Stateless Component using Recompose

Share this video with your friends

Send Tweet

Learn how to use the 'lifecycle' higher-order component to conveniently use hooks without using a class component.

Tim Kindberg
Tim Kindberg(instructor)
~ 8 years ago

I updated the example to use the state property instead of getInitialState method because Recompose updated their library to remove all usages of React.createClass.

Marc Schneider
Marc Schneider
~ 8 years ago

Could you please explain the benefit of doing it this way instead of using React.Component?

Tim Kindberg
Tim Kindberg(instructor)
~ 7 years ago

There is absolutely nothing wrong with using React.Component especially for one-off lifecycle connections.

But if you are already writing with a lot of HOC patterns or if you'd like to create a reusable HOC that needs to use a hook, I like the succinct API of recompose's lifecycle helper. I like not having to write the render method and spreading the props; it's just boilerplate noise IMO.

I personally have a love for simple functional stateless components that are decorated by composed higher-order components. So moving the hook from the component allows me to still use a functional stateless component as well.