4 ms·
The problem I have with react is that if I set a variable using a hook, it's asynchronous and not immediately available in my code. It's nice for updating the D
by alana314 3y ago
The problem I have with react is that if I set a variable using a hook, it's asynchronous and not immediately available in my code. It's nice for updating the DOM but causes me lots of race conditions.
- joshribakoff 3y agoThis is by design, the race conditions are caused by your implementation not complying with the constraints it was designed for you to stay within. Use a local variable (not react state) if you need to store a value and read it back synchronously. Use a useEffect hook if you need side effects to run when react state changes, and finally useMemo if you need derived react state
- boringuser2 3y agoThis is an issue of you not understanding how to properly manage the state of your application, not React. It's very likely that you're using effects improperly.
- throwaway4837 3y agoEffects are way too easy to use improperly. I used to be a fan of useEffect when it came out because of its concision, but after working with countless devs/agencies, it's clear that most people misuse it. The rules of useEffect are inelegant, and creating readable state machines with useEffect is very hard. You end up with spaghetti code either in your custom hook, or directly in the component. While most components need 2-3 effects rather than 20, useEffect is not a scalable way of managing state IMO.
- soledades 3y agohttps://react.dev/learn/you-might-not-need-an-effect https://react.dev/learn/you-might-not-need-an-effect