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

Prevent infinite re-renders by memoizing the useTranslation hook #743

Merged
merged 2 commits into from
Mar 24, 2022

Conversation

ajmnz
Copy link
Contributor

@ajmnz ajmnz commented Dec 1, 2021

This PR aims to prevent infinite re-renders when adding the t function in the useEffect dependency array.

const { t } = useTranslation("ns");

useEffect(() => {
   // ...
}, [t]);

A reproducible demo of the issue is available on CodeSandbox.
Remove the t function from the dependency array to stop it from re-rendering

The issue can be completely prevented by memoizing the return value of the useTranslate hook.


I see this was once added by #574, but got removed because of #604. I believe this needs to somehow be reintroduced in the library, to avoid being overriding eslint rules constantly.

@aralroca
Copy link
Owner

Sorry, I deleted the canary branch and I close the PR. I open again.

@aralroca aralroca reopened this Feb 14, 2022
@bmvantunes
Copy link
Contributor

Hello @aralroca, sorry to bother you with this question :)

Are there plans to merge this PR? It would be nice for one use case I'm facing right now.

@aralroca aralroca merged commit 64da9c1 into aralroca:canary Mar 24, 2022
@aralroca
Copy link
Owner

@all-contributors add @ajmnz for code

@allcontributors
Copy link
Contributor

@aralroca

I've put up a pull request to add @ajmnz! 🎉

@duc-gp
Copy link
Contributor

duc-gp commented Apr 11, 2022

there was a known issue stated by the author and with this PR the same issue is introduced again. I opened a issue here #834

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.

4 participants