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
Because useUnmount use useEffect with deps====[]. The first rendered callback console.log("comp unmount", 0); was cached and won't update. So in the above example, the output will always be 0 rather than the latest count value.
Describe the solution you'd like
A clear and concise description of what you want to happen.
There are two versions on useUnmount, one is from react-use and another is wroten by useRef and useEffect.
You should
Click show / hide button to mount the counter component.
Click + button to change the value
Click show /hide button again to unmount the counter
See console logs
Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.
useUnmount should cache callback by the second param deps as same as other hooks like useEffect, useCallback and etc.
useUnmount(callback) means callback won't be cached and the latest rendered callback will be called. useUnmount(callback, [count] means callback cache will update if count changed. useUnmount(callback, []) mans callback will be cached for the first time rendering and won't update anymore.
## [13.8.2](v13.8.1...v13.8.2) (2019-11-22)
### Bug Fixes
* **#792:** make useUnmount invoke the current callback version instead of very first ([75284c6](75284c6)), closes [#792](#792)
Is your feature request related to a problem? Please describe.
Imagining you implemented a counter component and would like to log the latest
count
when component unmount.Because
useUnmount
useuseEffect
withdeps====[]
. The first rendered callbackconsole.log("comp unmount", 0);
was cached and won't update. So in the above example, the output will always be0
rather than the latestcount
value.Describe the solution you'd like
A clear and concise description of what you want to happen.
I create a demo at https://codesandbox.io/s/funny-dirac-z0cl9
There are two versions on
useUnmount
, one is fromreact-use
and another is wroten byuseRef
anduseEffect
.You should
show / hide
button to mount the counter component.+
button to change the valueshow /hide
button again to unmount the counterDescribe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.
useUnmount
should cache callback by the second paramdeps
as same as other hooks likeuseEffect
,useCallback
and etc.useUnmount(callback)
means callback won't be cached and the latest rendered callback will be called.useUnmount(callback, [count]
means callback cache will update ifcount
changed.useUnmount(callback, [])
mans callback will be cached for the first time rendering and won't update anymore.cc: @byunicorn
The text was updated successfully, but these errors were encountered: