Skip to content

Commit

Permalink
[react-dom] Add support for createRoot(document) in Canary and Expe…
Browse files Browse the repository at this point in the history
…rimental release channels (DefinitelyTyped#68892)
  • Loading branch information
eps1lon authored Mar 5, 2024
1 parent 6229418 commit a0cdcf3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
4 changes: 4 additions & 0 deletions types/react-dom/canary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,8 @@ declare module "./client" {
interface HydrationOptions {
formState?: ReactFormState | null;
}

interface DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_CREATE_ROOT_CONTAINERS {
document: Document;
}
}
20 changes: 17 additions & 3 deletions types/react-dom/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,25 @@ export interface Root {
}

/**
* Replaces `ReactDOM.render` when the `.render` method is called and enables Concurrent Mode.
* Different release channels declare additional types of ReactNode this particular release channel accepts.
* App or library types should never augment this interface.
*/
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_CREATE_ROOT_CONTAINERS {}

export type Container =
| Element
| DocumentFragment
| DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_CREATE_ROOT_CONTAINERS[
keyof DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_CREATE_ROOT_CONTAINERS
];

/**
* createRoot lets you create a root to display React components inside a browser DOM node.
*
* @see https://reactjs.org/docs/concurrent-mode-reference.html#createroot
* @see {@link https://react.dev/reference/react-dom/client/createRoot API Reference for `createRoot`}
*/
export function createRoot(container: Element | DocumentFragment, options?: RootOptions): Root;
export function createRoot(container: Container, options?: RootOptions): Root;

/**
* Same as `createRoot()`, but is used to hydrate a container whose HTML contents were rendered by ReactDOMServer.
Expand Down
5 changes: 5 additions & 0 deletions types/react-dom/test/canary-tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,8 @@ function formTest() {
const formState = [1, "", "", 0] as unknown as ReactDOMClient.ReactFormState;
ReactDOMClient.hydrateRoot(document.body, <Page1 />, { formState });
}

function createRoot(validContainer: Element | DocumentFragment | Document) {
ReactDOMClient.createRoot(document);
ReactDOMClient.createRoot(validContainer);
}
3 changes: 1 addition & 2 deletions types/react-dom/test/react-dom-tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,7 @@ function createRoot() {
root.render(<div>initial render</div>);
root.render(false);

// only makes sense for `hydrateRoot`
// @ts-expect-error
// Will not type-check in a real project but accepted in DT tests since experimental.d.ts is part of compilation.
ReactDOMClient.createRoot(document);
}

Expand Down

0 comments on commit a0cdcf3

Please sign in to comment.