Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(hydrateRoot): to check and add validations for type check of arguments in hydrateRoot while development #28934

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMRoot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,37 @@ describe('ReactDOMRoot', () => {
);
});

it('warn if options is passed as second argument to hydrateRoot', async () => {
expect(() =>
ReactDOMClient.hydrateRoot(container, {
onUncaughtError: error => {
console.error(error);
},
}),
).toErrorDev(
'Warning: You passed an options object as the second argument to `hydrateRoot(...)`, did you forget to pass a React element?',
{withoutStack: true},
);
});

it('warn if element is passed as options to hydrateRoot', async () => {
expect(() =>
ReactDOMClient.hydrateRoot(
container,
<div>
<span />
</div>,
<div>
<span />
</div>,
),
).toErrorDev(
'You passed a JSX element as an options to hydrateRoot. You probably meant to ' +
'call root.render instead. ',
{withoutStack: true},
);
});

it('warn if JSX passed to createRoot', async () => {
function App() {
return 'Child';
Expand Down
24 changes: 24 additions & 0 deletions packages/react-dom/src/client/ReactDOMRoot.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,30 @@ export function hydrateRoot(
'Example usage: hydrateRoot(domContainer, <App />)',
);
}
if (
typeof initialChildren === 'object' &&
initialChildren !== null &&
(!('$$typeof' in initialChildren) ||
(initialChildren: any).$$typeof !== REACT_ELEMENT_TYPE)
) {
console.error(
'Warning: You passed an options object as the second argument to `hydrateRoot(...)`, did you forget to pass a React element?',
);
}

if (
typeof options === 'object' &&
options !== null &&
(options: any).$$typeof === REACT_ELEMENT_TYPE
) {
console.error(
'You passed a JSX element as an options to hydrateRoot. You probably meant to ' +
'call root.render instead. ' +
'Example usage:\n\n' +
' let root = hydrateRoot(domContainer, <App />, {...options});\n' +
' root.render(<App />);',
);
}
}

// For now we reuse the whole bag of options since they contain
Expand Down