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

onestopjs commented Sep 7, 2019

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

This comment has been minimized.

Copy link
Contributor Author

onestopjs commented Sep 7, 2019

This is a fix for #934

@coveralls

This comment has been minimized.

Copy link

coveralls commented Sep 7, 2019

Coverage Status

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

@jamuhl

This comment has been minimized.

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

This comment has been minimized.

Copy link
Contributor Author

onestopjs commented Sep 8, 2019

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
4 checks passed
4 checks passed
ci/circleci: build Your tests passed on CircleCI!
Details
codeclimate All good!
Details
coverage/coveralls Coverage increased (+0.01%) to 95.202%
Details
workflow Workflow: workflow
Details
@jamuhl

This comment has been minimized.

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
Projects
None yet
3 participants
You can’t perform that action at this time.