Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Remove the warning for setState on unmounted components in React

function useAsync(asyncFn, onSuccess) {
  useEffect(() => {
    let isActive = true;
    asyncFn().then(data => {
      if (isActive) onSuccess(data);
    });
    return () => { isActive = false };
  }, [asyncFn, onSuccess]);
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #Remove #warning #setState #unmounted #components #React
ADD COMMENT
Topic
Name
2+2 =