So there it goes. React v16.8 introduced hooks, a way to use state and other React features without writing classes. Folks who started a new project or managed to migrate their existing project already have access to use hooks!
We've recently managed to migrate to React 16.8, but still, haven't made it to Hooks yet.
I didn't think it would be a big deal
Some weeks after the migration, I had to implement a feature that shows a modal to the users and allows them to withdraw from all the jobs that they applied to. I had already used hooks in a project before and was anxious to start creating some awesome hooks! Using this article, I made a custom useCurrentUser
hook which looks neat, and when I needed state, I got my hands on useState
instead of converting the component to a class.
When I first made the PR, to my surprise, none of my Senior coworkers understood what I was doing!
The offender
Due to the way the project was made, we do data fetching inside the component. How do we do it with Hooks? Checking on the docs, I used the following workaround:
useEffect(() => {
let ignore = false;
getSubscriptions({userId}).then(({isSeeking, sendNewsTips}) => {
if (ignore) {
return;
}
setIsSeeking(isSeeking);
setNewsTips(sendNewsTips);
setOriginalIsSeeking(sendNewsTips);
});
return () => {
ignore = true;
};
}, [userId]);
Overall they didn't like the idea, but this piece of code threw them off. The code just seemed too cryptical to anyone reading it, particularly the ignore
variable and how it is being used. Seems like using a class would make it look a lot cleaner.
The reasons against Hooks
I took notes of their main concerns and made the following list:
- Introducing hooks is a big change to the project that requires team discussion to check if we should start writing all the new components using hooks.
- Hooks are still new, incomplete, and encourages code like the above which is too cryptical in comparison with the class-based way to do it.
- Hooks might be the future, but they're not the present. There're a lot of components written using classes and switching between them makes it hard to be productive (NOTE: they don't agree with this tweet).
- Sometimes it might be faster to deliver the feature using a class-based approach rather than writing hooks.
Thoughts?
It makes sense to me that if the team is not onboard with hooks yet, it might be better to hold it off. Eventually, hopefully, it might be reconsidered later on.
Do you/your team use Hooks? Why or Why not?
Top comments (2)
We do. There are only three of us in the team using React, which probably makes it easier to introduce change. One team member (voluntarily) tried out hooks over a weekend, and then enthusiastically showed the others how they worked. We're all using hooks for new code now. (There's no plan to convert old code - it still works fine.) So yes, you need some team discussion before bringing a big new thing into a shared codebase, but I'm sure that once a few team members understand hooks, it won't be an issue.
no, we don't need, cuz we have huarck