Skip to content

setLocale function is not stable #684

@vchirikov

Description

@vchirikov

Version

5.24.3

Describe the bug

Hi @ivanhofer, first of all thanks for the awesome lib.
I found the issue with setLocale with react - it can't be used as dependency in useEffect for example, because it causes:

Warning: Maximum update depth exceeded. This can happen when a component calls setState inside useEffect, but useEffect either doesn't have a dependency array, or one of the dependencies changes on every render.

the code:

const { setLocale } = useI18nContext();
useEffect(() => {
    setLocale('de');
    return () => setLocale('en');
  }, [setLocale]);

Ofc I can not provide setLocale as dependency, but I think this is a bug, and setLocale must be stable as setState.

Reproduction

git clone https://github.com/vchirikov/typesafe-i18n-issue-repro
cd typesafe-i18n-issue-repro
npm i && npm run start

Logs

No response

Config

No response

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions