4 ms·
this is the single biggest problem with React hooks, people overusing useEffect: const [sortedValues, setSortedValues] = useState([]) useEffect(() => {
by DanielHB 3y ago
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 avoided
To 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.
- carbotaniuman 3y agoWhat'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?
- 5Qn8mNbc2FNCiVV 3y agoIgnoring 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.