Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Remove the warning for setState on unmounted components in React

useEffect(() => {
  let isMounted = true;               // note mutable flag
  someAsyncOperation().then(data => {
    if (isMounted) setState(data);    // add conditional check
  })
  return () => { isMounted = false }; // cleanup toggles value, if unmounted
}, []);                               // adjust dependencies to your needs
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #Remove #warning #setState #unmounted #components #React
ADD COMMENT
Topic
Name
5+1 =