A Visual Guide to React's useEffect (2021)
Lots of people seem to misunderstand useEffect. It's not for transforming local state or for handling user events. In fact, there are many many reasons that people use useEffect for where it's not needed, and the recent React beta docs address this [0]. Their categories include:- Updating state based on props or state- Caching expensive calculations- Resetting all state when a prop changes- Adjusting some state when a prop changes- Sharing logic between event handlers- Sending a POST request- Chains of computations- Initializing the application- Notifying parent components about state changes- Passing data to the parent- Subscribing to an external store- Fetching dataEffects should also be idempotent, they should include pure functions that don't have side effects (such as doing a POST request) which Dan Abramov of the core React team addresses [1].[0] https://beta.reactjs.org/learn/you-might-not-need-an-effect[1] https://twitter.com/dan_abramov/status/1281669881667162112