useContext hook with IntlContext returns new reference every time issue #63
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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