useState is unsafe
See original GitHub issueuseState
return “unsafe” setter. If a component has been unmounted and the setter is called we will get a warning. In a class component we can handle unmount event and avoid this situation.
Example: https://codesandbox.io/s/vmm13qmw67
Click the button and get a warning in the console.
Cleanup logic will be very complicated in this case. Because we should separate unmount and “after render” cleanup.
Issue Analytics
- State:
- Created 5 years ago
- Comments:14 (8 by maintainers)
Top Results From Across the Web
React Hooks - How to use state safely
Hi, Daniel, let's talk about "How to use state safely" today? Daniel: Safe? Is it dangerous to use the state of React Hooks?...
Read more >Is it okey to use side effects in the useState hook callback?
It is not ok to use side effects inside the updater function. It might affect the render process, depending on the specific side...
Read more >React.Basic.Hooks - Pursuit - PureScript
This is unsafe because it allows arbitrary effects to be performed during a render, which may cause them to be run many times...
Read more >You're overusing useMemo: Rethinking Hooks memoization
In some cases, useMemo is irrelevant, overused, and harmful to your application performance. Learn these situations and how to avoid them.
Read more >David K. on Twitter: "⚛️ Here's a fun React tip: `useReducer ...
Here's a fun React tip: `useReducer` is a better `useState`, and it's easier to adopt than you may think. ... They're overused and...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Edit: this is probably better: https://github.com/facebook/react/issues/14113#issuecomment-467492761
Not like this, no. You can do this though.
Longer term, Suspense will be the recommended solution instead.
For what it’s worth this is probably better because it can work for multiple fetches (e.g. if route params change):