mirror of
https://github.com/HSLdevcom/digitransit-ui
synced 2026-01-30 10:25:29 +01:00
25 lines
723 B
JavaScript
25 lines
723 B
JavaScript
import React, { createContext, useContext } from 'react';
|
|
import PropTypes from 'prop-types';
|
|
import { intlShape } from 'react-intl';
|
|
|
|
const IntlContext = createContext();
|
|
|
|
export const IntlContextProvider = ({ intl, children }) => (
|
|
<IntlContext.Provider value={intl}>{children}</IntlContext.Provider>
|
|
);
|
|
|
|
IntlContextProvider.propTypes = {
|
|
intl: intlShape.isRequired,
|
|
children: PropTypes.node.isRequired,
|
|
};
|
|
|
|
export const useTranslationsContext = () => {
|
|
const intl = useContext(IntlContext);
|
|
if (!intl) {
|
|
throw new Error(
|
|
'useTranslationsContext must be used within an IntlContextProvider. ' +
|
|
'Make sure your component is wrapped in StoreListeningIntlProvider.',
|
|
);
|
|
}
|
|
return intl;
|
|
};
|