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

useContext hook returns new reference every time it is called #62

Open
sitaram-mulik-synacor opened this issue Dec 27, 2023 · 0 comments

Comments

@sitaram-mulik-synacor
Copy link

sitaram-mulik-synacor commented Dec 27, 2023

useContext hook with IntlContext creates an object with a new reference from the passed props every time it's called.

Problem:
If we use this hook directly or indirectly through any other hook inside a component which is getting iterated over a loop, results in performance issues due to unnecessary rendering.

Solution:
I think the ideal solution for this problem would be to replace the existing logic of the IntlProvider component (which uses local variables with new reference derived from the props passed) with a component state and pass it as context value. This way we can make use of useEffect hook for side effect logics like merging the properties of definition prop. Other approach could be to make use of useMemo hook, but that way we can not run separate logic based on different side effects.

Slackblitz link where the issue is reproducible:
https://stackblitz.com/edit/preact-starter-template-6lb3ag?file=index.js

PR
#63

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

No branches or pull requests

1 participant