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

Fix hooks being called conditionally in useTranslation #935

Merged
merged 1 commit into from
Sep 9, 2019

Conversation

onestopjs
Copy link
Contributor

In the useTranslation hook, useContext is called conditionally, which breaks the Rule of Hooks, so it may cause problems in the future. This fixes it by calling useContext on every render, just not always using the result from it.

In the `useTranslation` hook, `useContext` is called conditionally, which breaks the Rule of Hooks, so it may cause problems in the future. This fixes it by calling `useContext` on every render, just not always using the result from it.
@onestopjs
Copy link
Contributor Author

This is a fix for #934

@coveralls
Copy link

Coverage Status

Coverage increased (+0.01%) to 95.202% when pulling 3d2361f on PsychedMagnet:patch-1 into f41a24b on i18next:master.

@jamuhl
Copy link
Member

jamuhl commented Sep 8, 2019

Guess that will work - and also not against a merge.

But I would love to know how this can happen. The result from getHasUsedI18nextProvider() is always the same depending of using I18nextProvider or not, so how could there be a change of order? Only way i could think of is nesting I18nextProvider inside a useTranslation|withTranslation ?!?

@onestopjs
Copy link
Contributor Author

Oh, you are right. There is a goof up on my part - I was calling useTranslation above I18nextProvider. I will definitely fix this, thank you! :)

Anyways, I think unconditionally calling useContext is the better option as a good practice either way.

@jamuhl jamuhl merged commit b9c1b1c into i18next:master Sep 9, 2019
@jamuhl
Copy link
Member

jamuhl commented Sep 9, 2019

published in react-i18next@10.12.3

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.

3 participants