5 ms·
I disagree with 4 as well Maybe the author just used a poor example, but his useToggle is actually less readable to me. The initial example with just useState
by pigcat 5y ago
I disagree with 4 as well
Maybe the author just used a poor example, but his useToggle is actually less readable to me.
The initial example with just useState is super obvious to understand what's going on in that component.
With his useToggle example, if I'm in this codebase I'm now asking myself - does toggle() fire some hidden side effect? Now I need to go check out the hook to make sure.
- franciscop 5y agoWell that's the goal, that you should not really care (too much) how it's exactly implemented to use it, right? It could be very simple or very complex, it could be in Redux or a simple state hook. In the case of more serious side effects like if it persists through localStorage, you read it once (hopefully through some docs!) and then you know how to use it anywhere. I agree with you that in the author use case it's waaay too simple and the best case is to use it inline, but I believe what they meant to do (hopefully!) is use that as an example of a potentially more complex hook and how you can/should use custom hooks instead of shoving all the logic in the component.
- pigcat 5y agoFair point.