From e0dfde6af4210808f08aad32b54707f79f6f88fc Mon Sep 17 00:00:00 2001 From: Long Ho Date: Wed, 15 Mar 2023 00:05:19 -0400 Subject: [PATCH] feat(react-intl): memoize Context into global This is primarily dealing with packaging systems where multiple copies of react-intl might exist --- .../react-intl/src/components/injectIntl.tsx | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/react-intl/src/components/injectIntl.tsx b/packages/react-intl/src/components/injectIntl.tsx index c0bd548145..16e8d61179 100644 --- a/packages/react-intl/src/components/injectIntl.tsx +++ b/packages/react-intl/src/components/injectIntl.tsx @@ -7,8 +7,21 @@ function getDisplayName(Component: React.ComponentType): string { return Component.displayName || Component.name || 'Component' } -// TODO: We should provide initial value here -const IntlContext = React.createContext(null as any) +declare global { + interface Window { + __REACT_INTL_CONTEXT__: React.Context | 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( + null as any + )) + : React.createContext(null as any) const {Consumer: IntlConsumer, Provider: IntlProvider} = IntlContext export const Provider = IntlProvider