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

t is not refreshed when keyPrefix is changed #1542

Closed
halouvi opened this issue Aug 15, 2022 · 7 comments · Fixed by #1544
Closed

t is not refreshed when keyPrefix is changed #1542

halouvi opened this issue Aug 15, 2022 · 7 comments · Fixed by #1544

Comments

@halouvi
Copy link
Contributor

halouvi commented Aug 15, 2022

🐛 Bug Report

I wanted to use a dynamic keyPrefix in my component to support an A/B test we're running on the copy of a component.
When the component mounts, i fetch a new variant for the current user and so in the initial render, the keyPrefix is suffixed with an undefined that resolves to one of my variants as soon as the fetch is successful, way before the component is ever rendered on screen.

To Reproduce

codesandbox

Expected behavior

useTranslation should reset t when keyPrefix changes and reflect the new keyPrefix

The solution is simple:
Add keyPrefix to the dependency array of the useEffect starting in line 99 in useTranslation.js so that t is reset whenever keyPrefix changes.

Your Environment

  • runtime version: chrome
  • i18next version: 21.8.10
  • os: Mac
@adrai
Copy link
Member

adrai commented Aug 15, 2022

Would you like to send a Pull Request to address this?

@halouvi
Copy link
Contributor Author

halouvi commented Aug 15, 2022

Sure, i'd love to.

@halouvi
Copy link
Contributor Author

halouvi commented Aug 15, 2022

I couldn't create a new branch though.

@adrai
Copy link
Member

adrai commented Aug 15, 2022

@adrai
Copy link
Member

adrai commented Aug 15, 2022

try to solve it like this: 47d9670

@halouvi
Copy link
Contributor Author

halouvi commented Aug 15, 2022

I'm trying to understand why usePrevious was added in 47d9670.
I mean, there's this equality check previousJoinedNS !== joinedNS but as i understand it, since joinedNS is in the dependency array and is a string, that useEffect will not have even run again if previousJoinedNS === joinedNS.

If the namespaces array itself was placed in the dependency array it would have made more sense to me because the object reference would have change but since its passed as a string i don't really see the purpose.

@adrai
Copy link
Member

adrai commented Aug 15, 2022

#1517 (comment)

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 a pull request may close this issue.

2 participants