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

move removeItem to useCallback #69

Merged
merged 1 commit into from
Aug 8, 2024

Conversation

iamkd
Copy link
Contributor

@iamkd iamkd commented Aug 7, 2024

This PR wraps removeItem with useCallback to preserve its reference in most use cases.

Currently, removeItem does not have a stable reference. It is being re-initialized every time a value is changed. This is suboptimal, because calling removeItem should not re-initialize itself.

An example use case would be using it in a useEffect:

useEffect(() => {
  if (!user) {
    removeItem(); // causes infinite useEffect loop
  }
}, [user, removeItem]); 

While obviously there are ways to mitigate that, do extra checks etc., maintaining references as stable as possible should reduce possible bugs.

@astoilkov astoilkov merged commit 2c604b2 into astoilkov:main Aug 8, 2024
2 checks passed
@astoilkov
Copy link
Owner

Ah, yes.

Thank you for your work!

@iamkd iamkd deleted the fix-remove-item-stable branch August 8, 2024 09:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants