Comments (5)
Thanks @payapula! I'll be removing this from the workshop in the future :)
from advanced-react-apis.
IMHO the are actual (but insignificantly small) memory leaks were/are real, but the bigger problem was that 99.9% of workarounds to get rid of the warning (including the mounted
ref in this workshop) could not do anything about any memory leaks, it only silenced the warning itself and made the code more complex...
the pattern can be still useful to have in our tool belts, but no idea what is the best scenario to explain it if dispatch
/ setState
after unmount won't have to be guarded any more ¯\_(ツ)_/¯
if (isMounted) {
doSomethingThatMakesSenseOnlyIfMounted()
}
from advanced-react-apis.
...maybe starting some imperative animation for "payment confirmed" after 200 response from a POST request, that would only make sense if still mounted
and perhaps a counter-example to remove some imperative DOM event listeners - those should always be in the cleanup of a useEffect without any guarding, event listeners have to be removed unconditionally, especially if React is unmounting / already unmounted 😅
from advanced-react-apis.
In your workshop material you mentioned "Also notice that while what we're doing here is still useful and you'll learn valuable skills"
function useSafeDispatch(dispatch) {
const mountedRef = React.useRef(false)
React.useEffect(() => {
mountedRef.current = true
return () => {
mountedRef.current = false
}
}, [])
return React.useCallback(
(...args) => (mountedRef.current ? dispatch(...args) : void 0),
[dispatch],
)
}
But As mentioned on the link reactwg/react-18#82
"The workaround is worse than the original problem
The workaround above is very common. But not only is it unnecessary, it also makes things a bit worse:
In the future, we'd like to offer a feature that lets you preserve DOM and state even when the component is not visible, but disconnect its effects. With this feature, the code above doesn't work well. You'll "miss" the setPending(false) call because isMountedRef.current is false while the component is in a hidden tab. So once you return to the hidden tab and make it visible again, it will look as if your mutation hasn't "come through". By trying to evade the warning, we've made the behavior worse.
In addition, we've seen that this warning pushes people towards moving mutations (like POST) into effects just because effects offer an easy way to "ignore" something using the cleanup function and a flag. However, this also usually makes the code worse. The user action is associated with a particular event — not with the component being displayed — and so moving this code into effect introduces extra fragility. Such as the risk of the effect re-firing when some related data changes. So the warning against a problematic pattern ends up pushing people towards more problematic patterns though the original code was actually fine."
Please tell what should we do?
from advanced-react-apis.
Don't worry about it unless it becomes a problem in your app (it probably won't).
from advanced-react-apis.
Related Issues (20)
- Start the TypeScript-fication process by creating a `ts/main` branch HOT 6
- Warning not triggering from exercise 2, extra credit 3 HOT 1
- edit
- Another potential exercise 2 extra credit HOT 2
- Exercise 4 enhancement HOT 8
- Tests doesn't pass for `extra-1` in exercise 6 HOT 1
- Broken reference link in the documentation HOT 1
- Broken link to `How to contribute to open source`
- Changelog HOT 1
- useContext gotchas HOT 1
- React DevTools only works on isolated page HOT 3
- Node 17 HOT 1
- Exercise 4 (useLayoutEffect: auto-scrolling textarea) -> no discernible difference between useEffect and useLayoutEffect in React 18
- useLayoutEffect: auto-scrolling textarea HOT 1
- Docker compose for advanced-react-hooks fails HOT 4
- useDebugValue - production deploys don't show anything HOT 2
- Please tell why did you say its still valuable to apply useSafeDispatch() function in your material even after react 18? HOT 1
- useDebugValue: useMedia (Advanced React Hooks) HOT 2
- [docs] Update repo docs pointing to react old link to new link.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from advanced-react-apis.