Hacker News new | past | comments | ask | show | jobs | submit login

Not 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.



There 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.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: