question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useTranslation hook return object is not memoized

See original GitHub issue

The 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:closed
  • Created a year ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
gurkerl83commented, Apr 7, 2022

@aralroca Tested the canary release, works so far!

1reaction
gurkerl83commented, Apr 7, 2022

@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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found