3 ms·
I think I disagree, or at least we might disagree on what operations are common in React. When I have seen useEffect used, it's most often uneccesary. People a
by Gunax 4y ago
I think I disagree, or at least we might disagree on what operations are common in React.
When I have seen useEffect used, it's most often uneccesary. People are using it to create dependent state (ie. when x changes, then recalculate y) or to handle the result of an event.
It's really only needed in components that handle fetch responses, which IME is much fewer than 99.9%.
- larsnystrom 4y agoNot 99.9% of components, but 99.9% of applications. Of course I haven’t actually done the numbers, but if your application fetches data or listens to browser events you will need useEffect. I’d estimate 99.9% of web apps using React do at least one of those things.
- Capricorn2481 4y agoThere is only one case where you should be fetching inside of useEffect, and that is with an empty dependency array to get data on first render. Pretty simple and easy to wrap your head around. Everything else is triggered by event handlers. I work on a 70Loc react app and useEffect is used maybe once outside of this context. You really don't need it. And I don't know what you mean by "listening to browser events" with useEffect. You really just need to use onClick and onChange. You also don't need it to set state, you need calculated constant assignments.