Skip to content

Commit 21ca217

Browse files
authored
[React 19] Update OSS package imports for consistency (#1944)
Why Pro package uses named imports but OSS still used namespace imports, creating inconsistency across the codebase. Summary Converts 3 OSS files from namespace imports (import * as React) to named imports (createElement, isValidElement), matching Pro package style and React 19 best practices. Key improvements - Eliminates React namespace references in favor of direct imports - Improves tree-shaking by explicitly importing only what's used - Achieves full codebase consistency between OSS and Pro packages Impact Existing: No changes - both import styles work identically New: OSS package follows same pattern as Pro package Risks None. Import style change only, no runtime behavior differences.
1 parent ea7de0a commit 21ca217

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

packages/react-on-rails/src/createReactOutput.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import * as React from 'react';
1+
import { createElement, isValidElement, type ReactElement } from 'react';
22
import type { CreateParams, ReactComponent, RenderFunction, CreateReactOutputResult } from './types/index.ts';
33
import { isServerRenderHash, isPromise } from './isServerRenderResult.ts';
44

55
function createReactElementFromRenderFunctionResult(
66
renderFunctionResult: ReactComponent,
77
name: string,
88
props: Record<string, unknown> | undefined,
9-
): React.ReactElement {
10-
if (React.isValidElement(renderFunctionResult)) {
9+
): ReactElement {
10+
if (isValidElement(renderFunctionResult)) {
1111
// If already a ReactElement, then just return it.
1212
console.error(
1313
`Warning: ReactOnRails: Your registered render-function (ReactOnRails.register) for ${name}
@@ -19,7 +19,7 @@ work if you return JSX. Update by wrapping the result JSX of ${name} in a fat ar
1919
}
2020

2121
// If a component, then wrap in an element
22-
return React.createElement(renderFunctionResult, props);
22+
return createElement(renderFunctionResult, props);
2323
}
2424

2525
/**
@@ -88,5 +88,5 @@ export default function createReactOutput({
8888
return createReactElementFromRenderFunctionResult(renderFunctionResult, name, props);
8989
}
9090
// else
91-
return React.createElement(component as ReactComponent, props);
91+
return createElement(component as ReactComponent, props);
9292
}

packages/react-on-rails/src/handleError.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import * as React from 'react';
1+
import { createElement } from 'react';
22
import { renderToString } from './ReactDOMServer.cts';
33
import type { ErrorOptions } from './types/index.ts';
44
import generateRenderingErrorMessage from './generateRenderingErrorMessage.ts';
55

66
const handleError = (options: ErrorOptions): string => {
77
const msg = generateRenderingErrorMessage(options);
8-
const reactElement = React.createElement('pre', null, msg);
8+
const reactElement = createElement('pre', null, msg);
99
return renderToString(reactElement);
1010
};
1111

packages/react-on-rails/src/serverRenderReactComponent.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import * as React from 'react';
2-
import type { ReactElement } from 'react';
1+
import { isValidElement, type ReactElement } from 'react';
32

43
// ComponentRegistry is accessed via globalThis.ReactOnRails.getComponent for cross-bundle compatibility
54
import createReactOutput from './createReactOutput.ts';
@@ -69,7 +68,7 @@ function processPromise(
6968
return '{}';
7069
}
7170
return result.then((promiseResult) => {
72-
if (React.isValidElement(promiseResult)) {
71+
if (isValidElement(promiseResult)) {
7372
return processReactElement(promiseResult);
7473
}
7574
return promiseResult;

0 commit comments

Comments
 (0)