Skip to content

Commit 57f91ed

Browse files
committed
Avoid wrapping non-React registrations
1 parent d466e91 commit 57f91ed

1 file changed

Lines changed: 31 additions & 9 deletions

File tree

react_on_rails_pro/spec/dummy/client/app/strictModeSupport.tsx

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ type RenderFunction = ((props?: unknown, railsContext?: unknown) => unknown) & {
1717
prototype?: ReactClassPrototype;
1818
};
1919

20-
type RegisteredComponent = string | ComponentWithMetadata | RenderFunction;
20+
type ReactElementTypeObject = {
21+
$$typeof?: unknown;
22+
displayName?: string;
23+
name?: string;
24+
};
25+
type StrictModeWrappableComponent = string | ComponentWithMetadata | RenderFunction | ReactElementTypeObject;
26+
type RegisteredComponent = StrictModeWrappableComponent | object;
2127
type ComponentRegistry = Record<string, RegisteredComponent>;
2228
type ReactOnRailsWithRegister = {
2329
register: (components: ComponentRegistry) => void;
@@ -26,9 +32,12 @@ type ReactOnRailsWithRegister = {
2632

2733
const STRICT_MODE_PATCHED = '__reactOnRailsProDummyStrictModePatched';
2834

29-
const wrappedFunctionComponents = new WeakMap<ComponentWithMetadata | RenderFunction, RegisteredComponent>();
35+
const wrappedFunctionComponents = new WeakMap<
36+
ComponentWithMetadata | RenderFunction,
37+
StrictModeWrappableComponent
38+
>();
3039
const wrappedRenderFunctions = new WeakMap<RenderFunction, RenderFunction>();
31-
const wrappedOtherComponents = new Map<RegisteredComponent, RegisteredComponent>();
40+
const wrappedOtherComponents = new Map<StrictModeWrappableComponent, StrictModeWrappableComponent>();
3241

3342
const isPromiseLike = (value: unknown): value is Promise<unknown> =>
3443
typeof value === 'object' &&
@@ -41,11 +50,11 @@ const isRenderFunction = (component: RegisteredComponent): component is RenderFu
4150
return false;
4251
}
4352

44-
if (component.prototype?.isReactComponent) {
53+
if ((component.prototype as ReactClassPrototype | undefined)?.isReactComponent) {
4554
return false;
4655
}
4756

48-
if (component.renderFunction) {
57+
if ('renderFunction' in component && component.renderFunction) {
4958
return true;
5059
}
5160

@@ -55,7 +64,12 @@ const isRenderFunction = (component: RegisteredComponent): component is RenderFu
5564
const isRendererFunction = (component: RegisteredComponent): component is RenderFunction =>
5665
isRenderFunction(component) && component.length === 3;
5766

58-
const createStrictModeWrapper = (Component: RegisteredComponent): React.FC<Record<string, unknown>> => {
67+
const isReactElementTypeObject = (component: RegisteredComponent): component is ReactElementTypeObject =>
68+
typeof component === 'object' && '$$typeof' in component && typeof component.$$typeof === 'symbol';
69+
70+
const createStrictModeWrapper = (
71+
Component: StrictModeWrappableComponent,
72+
): React.FC<Record<string, unknown>> => {
5973
function StrictModeWrapper(props: Record<string, unknown>) {
6074
const childElement =
6175
typeof Component === 'string'
@@ -74,7 +88,7 @@ const createStrictModeWrapper = (Component: RegisteredComponent): React.FC<Recor
7488
return StrictModeWrapper;
7589
};
7690

77-
const wrapComponentInStrictMode = (component: RegisteredComponent): RegisteredComponent => {
91+
const wrapComponentInStrictMode = (component: StrictModeWrappableComponent): StrictModeWrappableComponent => {
7892
if (typeof component === 'function') {
7993
const cachedComponent = wrappedFunctionComponents.get(component);
8094
if (cachedComponent) {
@@ -110,7 +124,7 @@ const wrapRenderFunctionResult = (result: unknown): unknown => {
110124
}
111125

112126
if (typeof result === 'function') {
113-
return wrapComponentInStrictMode(result as RegisteredComponent);
127+
return wrapComponentInStrictMode(result as StrictModeWrappableComponent);
114128
}
115129

116130
return result;
@@ -145,9 +159,17 @@ export const wrapRegisteredComponentsWithStrictMode = (components: ComponentRegi
145159
return [name, wrapRenderFunctionInStrictMode(component)];
146160
}
147161

162+
if (
163+
typeof component !== 'function' &&
164+
typeof component !== 'string' &&
165+
!isReactElementTypeObject(component)
166+
) {
167+
return [name, component];
168+
}
169+
148170
return [name, wrapComponentInStrictMode(component)];
149171
}),
150-
);
172+
) as ComponentRegistry;
151173

152174
export const enableStrictModeForReactOnRails = <T extends ReactOnRailsWithRegister>(reactOnRails: T): T => {
153175
if (reactOnRails[STRICT_MODE_PATCHED]) {

0 commit comments

Comments
 (0)