Don't set the state if an old promise resolves.

Easy way to make sure you don't set the state if promises resolve late

April 1, 2019 - 1 minute read -

Easy way to make sure you don’t set the state if an old promise resolves when the prop changes or the component unmounts.

const [someState, setSomeState] = useState(null);
useEffect(() => {
  let current = true;
  fetchSomething(someProp).then(result => {
    if(current) {
      setSomeState(result)
    }
  })
  return () => { current = false }
}, [someProp])