Skip to content

Commit

Permalink
feat(react-intl): memoize Context into global
Browse files Browse the repository at this point in the history
This is primarily dealing with packaging systems where multiple copies of react-intl might exist
  • Loading branch information
longlho committed Mar 15, 2023
1 parent aaeeae4 commit e0dfde6
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions packages/react-intl/src/components/injectIntl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,21 @@ function getDisplayName(Component: React.ComponentType<any>): string {
return Component.displayName || Component.name || 'Component'
}

// TODO: We should provide initial value here
const IntlContext = React.createContext<IntlShape>(null as any)
declare global {
interface Window {
__REACT_INTL_CONTEXT__: React.Context<IntlShape> | undefined
}
}

// This is primarily dealing with packaging systems where multiple copies of react-intl
// might exist
const IntlContext =
typeof window !== 'undefined'
? window.__REACT_INTL_CONTEXT__ ||
(window.__REACT_INTL_CONTEXT__ = React.createContext<IntlShape>(
null as any
))
: React.createContext<IntlShape>(null as any)
const {Consumer: IntlConsumer, Provider: IntlProvider} = IntlContext

export const Provider = IntlProvider
Expand Down

0 comments on commit e0dfde6

Please sign in to comment.