Skip to content

carloluis/use-intl-hook

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

use-intl-hook

React use-intl-hook for react-intl.

ℹ️ Now, you can simply use the useIntl available on react-intl. Check the API docs here

Install

yarn add use-intl-hook

Usage

import 'intl';
import React from 'react';
import ReactDOM from 'react-dom';
import { IntlProvider, addLocaleData } from 'react-intl';
import { InjectIntlContext, useIntl } from 'use-intl-hook';

import { locale, messages } from './i18n';

const Example = () => {
  // call the useIntl hook to get the intl object from react-intl
  const intl = useIntl();
  return <h1>{intl.formatMessage({ id: 'salutation' })}</h1>;
};

// Include `InjectIntlContext` up in the tree to store the intl object from react-intl
ReactDOM.render(
  <IntlProvider key={locale} locale={locale} messages={messages}>
    <InjectIntlContext>
      <Example />
    </InjectIntlContext>
  </IntlProvider>,
  document.querySelector('#app')
);