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

usePrevious only tracks the previous of the last render and is affected by other hooks #476

Closed
paul-sachs opened this issue Jul 19, 2019 · 2 comments · Fixed by #551
Closed
Labels

Comments

@paul-sachs
Copy link
Contributor

paul-sachs commented Jul 19, 2019

If i have other hooks in a component that are unrelated to what the usePrevious is tracking, some update that causes a rerender from that hook will sync the previous value to the current value. To see an example of this, here's a sandbox. To see the behavior, click + a few times and then click toggle on to toggle unrelated state. Not that the value for prev will update.

In this sandbox I've implemented an alternative usePrevious that tracks a more accurate version of previous.

I can make a PR but wanted to know if maybe we call this new version something else to support the previous version of usePrevious.

I understand that usePrevious tracks the previous render value, but this version may be more strictly useful in an application. Maybe usePreviousDistinct?

@streamich
Copy link
Owner

🎉 This issue has been resolved in version 11.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@hcharley
Copy link

hcharley commented Sep 1, 2019

Oh! I knew I've been wanting this hook without even knowing how to think about it. I just saw it pop in my VSCode autocomplete and realized this was just what I needed!

Thank you for this! @streamich @psachs21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants