Re:Making Sense of React Server Components
by technion on Wednesday September 06, 2023 @09:09PM
(#37413528)
Don't forget "functions that do totally normal things now need to be rewritten as a hook".
[ Parent
| on HN ]
Re:Making Sense of React Server Components
by paulddraper on Wednesday September 06, 2023 @09:13PM
(#37413558)
If you are writing functions that do totally normally things as hooks, you are indeed doing it wrong.
Hooks are for stateful code.
Re:Making Sense of React Server Components
by shortcake27 on Thursday September 07, 2023 @02:25AM
(#37415403)
Yeah I think that’s their point. People rewriting stateless functions as hooks because “hooks are cool” before they actually understand what they do.
Easiest way I put it to my team was that in order to qualify as a hook, it must consume a hook.
Re:Making Sense of React Server Components
by DanielHB on Thursday September 07, 2023 @03:33AM
(#37415805)
this is the single biggest problem with React hooks, people overusing useEffect:
const [sortedValues, setSortedValues] = useState([])
useEffect(() => {
setSortedValues(values.sort())
}, [values])
It is unfortunate that useEffect is too powerful of a escape hatch that people forget it IS a escape hatch and should be avoidedTo be honest I much prefer hooks than old class-based state manipulation, but it does require deeper understanding of the React, JS and state to get right.
Re:Making Sense of React Server Components
by carbotaniuman on Thursday September 07, 2023 @04:10PM
(#37424903)
What's the actual solution for that? I've only used Vue and Solid where "computeds" functions fall out of how signals or reactivity work, not sure how it works in React-land. Do you just leave it as a plain function and rely on React calling on your component?
Re:Making Sense of React Server Components
by 5Qn8mNbc2FNCiVV on Friday September 08, 2023 @05:40AM
(#37431542)
Ignoring that sort mutates the initial array which is a bug in that example, you would just sort it in the function body. If the sort takes a lot of time or the array is big and you don't want to create garbage, you memoize it with useMemo.