From 51cfc4c9c570646b0a9e64e8cde53c99473c15c5 Mon Sep 17 00:00:00 2001 From: Lenz Weber-Tronic Date: Mon, 5 Jun 2023 22:52:44 +0200 Subject: [PATCH 1/3] lazily create Context for RSC compat --- src/components/Context.ts | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/components/Context.ts b/src/components/Context.ts index cfba5537d..82c39899a 100644 --- a/src/components/Context.ts +++ b/src/components/Context.ts @@ -1,4 +1,4 @@ -import { createContext } from 'react' +import { Context, createContext } from 'react' import type { Action, AnyAction, Store } from 'redux' import type { Subscription } from '../utils/Subscription' import { StabilityCheck } from '../hooks/useSelector' @@ -13,13 +13,31 @@ export interface ReactReduxContextValue< stabilityCheck: StabilityCheck } -export const ReactReduxContext = - /*#__PURE__*/ createContext(null as any) +let realContext: Context | null = null +function getContext() { + if (!realContext) { + realContext = createContext(null as any) + if (process.env.NODE_ENV !== 'production') { + realContext.displayName = 'ReactRedux' + } + } + return realContext +} -export type ReactReduxContextInstance = typeof ReactReduxContext +export const ReactReduxContext = /*#__PURE__*/ new Proxy( + {} as Context, + new Proxy>>( + {}, + { + get(_, handler) { + const target = getContext() + // @ts-ignore + return (_target, ...args) => Reflect[handler](target, ...args) + }, + } + ) +) -if (process.env.NODE_ENV !== 'production') { - ReactReduxContext.displayName = 'ReactRedux' -} +export type ReactReduxContextInstance = typeof ReactReduxContext export default ReactReduxContext From 1b350fe52327522a50dbc5e3524212ab1c79b88d Mon Sep 17 00:00:00 2001 From: Lenz Weber-Tronic Date: Mon, 5 Jun 2023 23:16:44 +0200 Subject: [PATCH 2/3] Update src/components/Context.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Mateusz BurzyƄski --- src/components/Context.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/Context.ts b/src/components/Context.ts index 82c39899a..398d51565 100644 --- a/src/components/Context.ts +++ b/src/components/Context.ts @@ -26,7 +26,7 @@ function getContext() { export const ReactReduxContext = /*#__PURE__*/ new Proxy( {} as Context, - new Proxy>>( + /*#__PURE__*/ new Proxy>>( {}, { get(_, handler) { From aaf1364c576a53540584dff050247dba3485ddba Mon Sep 17 00:00:00 2001 From: Lenz Weber-Tronic Date: Mon, 5 Jun 2023 23:28:21 +0200 Subject: [PATCH 3/3] fixup import --- src/components/Context.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/components/Context.ts b/src/components/Context.ts index 398d51565..5c49d5a0e 100644 --- a/src/components/Context.ts +++ b/src/components/Context.ts @@ -1,4 +1,5 @@ -import { Context, createContext } from 'react' +import { createContext } from 'react' +import type { Context } from 'react' import type { Action, AnyAction, Store } from 'redux' import type { Subscription } from '../utils/Subscription' import { StabilityCheck } from '../hooks/useSelector'