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

Name collision with React Hooks #1

Open
jonasdlindner opened this issue Feb 2, 2021 · 1 comment
Open

Name collision with React Hooks #1

jonasdlindner opened this issue Feb 2, 2021 · 1 comment

Comments

@jonasdlindner
Copy link

I tried the example with React Version "17.0.1" (Changed Version in package.json) and got the following error

Failed to compile.

src/i18nInit.js
  Line 20:3:  React Hook "useLocale" cannot be called at the top level. React Hooks must be called in 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.
@jonasdlindner
Copy link
Author

jonasdlindner commented Feb 3, 2021

Solved by using another function-name in src/i18nInit.js

import { addLocale, useLocale as localize} from 'ttag';
import * as cookie from './cookie';

const LOCALE_COOKIE = '__locale';

function getLocale() {
  return cookie.get(LOCALE_COOKIE) || 'en';
}

export function saveLocale(locale) {
  cookie.set(LOCALE_COOKIE, locale);
}

// setup
const locale = getLocale();

if (locale !== 'en') {
  const translationsObj = require(`../i18n/${locale}.po.json`);
  addLocale(locale, translationsObj);
  localize(locale);
}

@jonasdlindner jonasdlindner changed the title Example not working with newer React Version Name collision with React Hooks Feb 3, 2021
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

1 participant