useTranslation hook return object is not memoized
See original GitHub issueThe return object of the useTranslation hook is not memoized, which means it’ll be a “new” object on every render.
This is especially an issue when the t function is included in the dependency array of useMemo, useCallback etc.
It can easily be reproduced like this:
import useTranslation from 'next-translate/useTranslation'
import { useMemo, useState } from 'react'
export const UseTranslationRerender: React.FC<{}> = () => {
const { t } = useTranslation('myroofs')
const [someValue, setSomeValue] = useState(false)
useMemo(() => {
console.log('useMemo')
}, [t])
return <button onClick={() => setSomeValue((prev) => !prev)}>Change State</button>
}
By the looks of it the object can be memoized based on the namespace.
Issue Analytics
- State:
- Created a year ago
- Comments:8 (7 by maintainers)
Top Results From Across the Web
useTranslation hook return object is not memoized · Issue #818
The return object of the useTranslation hook is not memoized, which means it'll be a "new" object on every render.
Read more >Should I memoize the returned object of a custom React hook?
I would assume that it would not create a new object everytime since it's composed with memoized methods and primitives. export const useToggle ......
Read more >useTranslation (hook) - react-i18next documentation
The useTranslation hook will trigger a Suspense if not ready (eg. pending load of translation files). You can set useSuspense to false if...
Read more >Should I memoize the returned object of a custom React hook?
You don't need to memoize the returned object unless you are passing the Object directly to function of useEffect in which case reference...
Read more >ok-react-use-form - npm Package Health Analysis - Snyk
useForm hook returns an object that has both input and $ keys with the ... object is mainly used for declaring form validations...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@aralroca Tested the canary release, works so far!
@aralroca I have provided a PR for the problem of not losing translations within page exit transitions. See the commit message for more details. The PR baseline is the canary branch.
https://github.com/vinissimus/next-translate/pull/829
Thx!