ttag-org / ttag

:orange_book: simple approach for javascript localization
https://ttag.js.org/
MIT License
345 stars 43 forks source link

API rename (maybe) or a quest for a workaround #176

Closed dimaqq closed 5 years ago

dimaqq commented 5 years ago

Current API has useLocale (as well as useLocales).

Using that within a functional react component (that which can use hooks), triggers following error:

Failed to compile
./src/App.js
  Line 65:  React Hook "useLocale" is called in function "set_locale" which is neither a React function component or a custom React Hook function  react-hooks/rules-of-hooks

Search for the keywords to learn more about each error.

(Here, set_locale is my own function).

It seems that whatever checker got installed with CRA considers use[A-Z].* to be a react hook 🤔

So far I have this workaround:

import {t, useLocale as foo} from "ttag";
...
const App = () => {
  ...
  const set_locale = name => {
    foo(name);
    ...
  }

  useEffect(
    async () => {
      ...
      set_locale();
    }, []);

  return ...

Perhaps there's a better workaround?

Or, if all else fails,

AlexMost commented 5 years ago

Hi @dimaqq! Thanks for the report. I think your workaround is fine to satisfy linter requirements.