News for hackers. Stuff that matters.
Re:Frustrations with React Hooks by ssalka on Tuesday September 10, 2019 @01:09PM (#20930510)
Coming from the FP world, hooks are a terror because they break all the nice rules functions are supposed to uphold. Hook components don't necessarily return the same value when given the same arguments, making them impure (stateful). The benefit of function components IMO has always been that you can use the function scope as a container for pure, stateless rendering logic. With the introduction of hooks I can no longer ensure that.
Re:Frustrations with React Hooks by aidos on Tuesday September 10, 2019 @01:22PM (#20930707)
Nothing stops you from using pure function components, and you should where possible. But eventually, you need to hold state somewhere. You don't need to put it in your components, but you do need to deal with it. If anything, hooks are a nice middle ground where the behaviour becomes declarative.
Re:Frustrations with React Hooks by ssalka on Tuesday September 10, 2019 @03:15PM (#20932273)
My personal opinion is that state should be contained in some sort of class or data structure, separate from functional architectures where the notion of state is generally avoided. So my usual suggestion would be to use either a class component (or redux or insert favorite state mgmt solution here) for your state, and then inject the state as props to your function components
Re:Frustrations with React Hooks by cercatrova on Thursday September 12, 2019 @02:10AM (#20948516)
That's fine, you can use React's Context API and reducers just like Redux, for which the useContext and useReducer hooks exist. Then it's just like Redux, inject a Provider and Consumer and you're good to go.
Re:Frustrations with React Hooks by PKop on Tuesday September 10, 2019 @01:55PM (#20931109)
How does the existence of hooks preclude you from using function components without state (without any hooks)?
Re:Frustrations with React Hooks by ssalka on Tuesday September 10, 2019 @03:12PM (#20932234)
They don't, but before I could look at a function component and go "ah, a nice stateless component" whereas now I have to wonder whether the _function_ contains _state_
Re:Frustrations with React Hooks by s_y_n_t_a_x on Tuesday September 10, 2019 @03:22PM (#20932400)
If you see "useWhatever"... then it has a hook.

And you already have to wonder if a function has state if you didn't write it. If you did, you should know.