question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useState is unsafe

See original GitHub issue

useState 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:closed
  • Created 5 years ago
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

10reactions
gaearoncommented, Feb 26, 2019

Edit: this is probably better: https://github.com/facebook/react/issues/14113#issuecomment-467492761

Not like this, no. You can do this though.

const isMounted = useRef(true);

useEffect(() => {
  performCall();
  return () => {
    isMounted.current = false;
  };
}, []);

async function performCall() {
    let newN = await apiCall(n)
    if (isMounted.current) {
        setN(newN);
    }
}

Longer term, Suspense will be the recommended solution instead.

5reactions
gaearoncommented, Feb 26, 2019

For what it’s worth this is probably better because it can work for multiple fetches (e.g. if route params change):

useEffect(() => {
  let canceled = false;

  async function performCall() {
    let newN = await apiCall(n)
    if (!canceled) {
      setN(newN);
    }
  }

  performCall();
  return () => {
    canceled = true;
  };
}, []);
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found