Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get the latest state in useUnmount ? #792

Closed
malash opened this issue Nov 22, 2019 · 2 comments · Fixed by #794
Closed

How to get the latest state in useUnmount ? #792

malash opened this issue Nov 22, 2019 · 2 comments · Fixed by #794
Assignees
Labels
bug Something isn't working released

Comments

@malash
Copy link

malash commented Nov 22, 2019

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.

const Comp = () => {
  const [count, setCount] = useState(0);
  useUnmount(() => {
    console.log("comp unmount", count);
  });
  return (
    <>
      <div>Count: {count}</div>
      <button onClick={() => setCount(count + 1)}>+</button>
    </>
  );
};

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.

I create a demo at https://codesandbox.io/s/funny-dirac-z0cl9

There are two versions on useUnmount, one is from react-use and another is wroten by useRef and useEffect.

You should

  1. Click show / hide button to mount the counter component.
  2. Click + button to change the value
  3. Click show /hide button again to unmount the counter
  4. 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.

cc: @byunicorn

@xobotyi
Copy link
Contributor

xobotyi commented Nov 22, 2019

It should not use the deps, but its definely a bug.

I'll take a look today.

@xobotyi xobotyi self-assigned this Nov 22, 2019
@xobotyi xobotyi added the bug Something isn't working label Nov 22, 2019
streamich added a commit that referenced this issue Nov 22, 2019
fix(#792): make useUnmount invoke the recent callback version
streamich pushed a commit that referenced this issue Nov 22, 2019
## [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)
@streamich
Copy link
Owner

🎉 This issue has been resolved in version 13.8.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants