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

locale switching does not trigger rerender #33

Open
asyncink opened this issue Jan 25, 2022 · 2 comments
Open

locale switching does not trigger rerender #33

asyncink opened this issue Jan 25, 2022 · 2 comments

Comments

@asyncink
Copy link

asyncink commented Jan 25, 2022

hello and thank you for your package!

we use it since first releases and happy now with removing state from provider and new ref implementation. bug with stale dict affected us too.

for now we noticed one more bug and it looks like it is connected with refs implementation.

the t function from useI18n hook is not changing when locale is switched.

so if we have something like:

const navigationItems = useMemo(
  () =>
    navigation.map(({ name, url }) => (
      <Link
        href={url}
        key={name}
      >
        {t(name)}
      </Link>
    )),
  [navigation, asPath, t]
)

return <nav>{navigationItems}</nav>

and then if we switch locale - the hook is not rerunning and page is not rerendered. workaround here is explicitly add locale from router to useMemo deps array:

const {locale} = useRouter()

const navigationItems = useMemo(
  () =>
    navigation.map(({ name, url }) => (
      <Link
        href={url}
        key={name}
      >
        {t(name)}
      </Link>
    )),
  [navigation, asPath, t, locale]
)

return <nav>{navigationItems}</nav>

but this workaround causes eslint-react-hooks error since locale is not used inside hook but still present in its deps array. it looks like t function should change in locale change.

in examples below assuming that navigation is something like:

const navigation = [
  {
    name: 'navigation.foo',
    url: '/foo'
  },
  {
    name: 'navigation.bar',
    url: '/bar'
  },
]

and our locale is:

const locale = {
  navigation: {
    foo: 'Foo Page',
    bar: 'Bar Page'
  }
}
@lopezjurip
Copy link
Member

Hi @asyncink

Looks like a valid reason to revert #25

In the meantime you can go back to version ^1.0.0 where t changes when locale changes.

@asyncink
Copy link
Author

asyncink commented Jan 28, 2022

Hi @lopezjurip

Thank you for your reply!

I think that implementation with refs is quite good since it fixes bug with stale dict during next page transitions. For the same reason we can't switch to 1.x.x version.

Unfortunately I have no time right now to figure out how to fix t func in next-rosetta codebase, but I will try later!

It looks like we should add locale to deps somewhere inside provider/hook and then return new t every time locale changes.

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

No branches or pull requests

2 participants