You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In this scenario, the SelectSearch has a getOptions property set. When this component flashes (i.e., it is unmounted and mounted rapidly), the following warning raises on the browser:
Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
I found in useFetch.js Line 22 that setFetching() was the state update that causes this warning.
Workaround
Add a new state mounted in useFetch(), add a useEffect managing the state mounted and fetch the option if is mounted.
exportdefaultfunctionuseFetch(...){const[mounted,setMounted]=useState(false);// Add a new state
...
...
constfetch=useMemo(()=>{constfilter=filterOptions||((op)=>()=>op);if(!getOptions){return(s)=>setOptions(flattenOptions(filter(defaultOptions)(s)));}if(!mounted){// If is not mounted, return immediatelyreturn(s)=>[];}returndebounce((s)=>{constoptionsReq=getOptions(s,defaultOptions);setFetching(true);Promise.resolve(optionsReq).then((newOptions)=>{setOptions(flattenOptions(filter(newOptions)(s)));}).finally(()=>setFetching(false));},debounceTime);},[filterOptions,defaultOptions,getOptions,debounceTime]);
...
...
useEffect(()=>{// Track the mounted statesetMounted(true);return()=>setMounted(false);),[]);return{ options, setOptions, fetching };}
Do you think this workaround is good enough for a pull request?
The text was updated successfully, but these errors were encountered:
Thanks for the report @tada-s . It's a good workaround but would also need to exist inside the debounced promise. A more robust solution would be to move the setOptions call to a function and have your mount check there.
In this scenario, the SelectSearch has a
getOptions
property set. When this component flashes (i.e., it is unmounted and mounted rapidly), the following warning raises on the browser:I found in useFetch.js Line 22 that
setFetching()
was the state update that causes this warning.Minimal working example
Workaround
Add a new state
mounted
inuseFetch()
, add a useEffect managing the state mounted and fetch the option if is mounted.Do you think this workaround is good enough for a pull request?
The text was updated successfully, but these errors were encountered: