fix: add i18n.language to translate callback deps for React Compiler compatibility #11103
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.
Related GitHub Issue
Closes: #11100
Description
This PR attempts to address Issue #11100. Feedback and guidance are welcome.
PR #9565 added React Compiler integration which automatically handles memoization in React applications. The issue is that the
TranslationContexthas a memoization bug that React Compiler exposes:The
translatecallback inTranslationContext.tsxdepends only on[i18n], buti18nis a singleton object whose reference never changes - only its internal language state changes wheni18n.changeLanguage()is called. This means thetranslatecallback was never recreated when the language changed, and React Compiler could aggressively cache the results of components using translations.Fix: Added
i18n.languageto the dependency array of theuseCallbackhook. This ensures thetranslatefunction is recreated when the language changes, causing all components usinguseAppTranslation()to properly re-render with updated translations.Test Procedure
TranslationContextpass:cd webview-ui && npx vitest run src/i18n/__tests__/TranslationContext.spec.tsxPre-Submission Checklist
Documentation Updates
Additional Notes
The eslint-disable comment is intentional - the
react-hooks/exhaustive-depsrule flagsi18n.languageas "unnecessary" because it is not used inside the callback body. However, this dependency is specifically needed to trigger re-memoization when the language changes, sincei18n.t()internally depends on the current language setting.Important
Fixes memoization bug in
TranslationContext.tsxby addingi18n.languagetouseCallbackdependencies, ensuring proper re-rendering on language change.TranslationContext.tsxby addingi18n.languagetouseCallbackdependencies.translatefunction is recreated when language changes, preventing React Compiler from over-caching.TranslationContextpass.This description was created by
for ac046a4. You can customize this summary. It will automatically update as commits are pushed.