react/packages/react-dom
Josh Story cb151849e1
[react-dom] move all client code to react-dom/client (#28271)
This PR reorganizes the `react-dom` entrypoint to only pull in code that
is environment agnostic. Previously if you required anything from this
entrypoint in any environment the entire client reconciler was loaded.
In a prior release we added a server rendering stub which you could
alias in server environments to omit this unecessary code. After landing
this change this entrypoint should not load any environment specific
code.

While a few APIs are truly client (browser) only such as createRoot and
hydrateRoot many of the APIs you import from this package are only
useful in the browser but could concievably be imported in shared code
(components running in Fizz or shared components as part of an RSC app).
To avoid making these require opting into the client bundle we are
keeping them in the `react-dom` entrypoint and changing their
implementation so that in environments where they are not particularly
useful they do something benign and expected.

#### Removed APIs
The following APIs are being removed in the next major. Largely they
have all been deprecated already and are part of legacy rendering modes
where concurrent features of React are not available
* `render`
* `hydrate`
* `findDOMNode`
* `unmountComponentAtNode`
* `unstable_createEventHandle`
* `unstable_renderSubtreeIntoContainer`
* `unstable_runWithPrioirty`

#### moved Client APIs
These APIs were available on both `react-dom` (with a warning) and
`react-dom/client`. After this change they are only available on
`react-dom/client`
* `createRoot`
* `hydrateRoot`

#### retained APIs
These APIs still exist on the `react-dom` entrypoint but have normalized
behavior depending on which renderers are currently in scope
* `flushSync`: will execute the function (if provided) inside the
flushSync implemention of FlightServer, Fizz, and Fiber DOM renderers.
* `unstable_batchedUpdates`: This is a noop in concurrent mode because
it is now the only supported behavior because there is no legacy
rendering mode
* `createPortal`: This just produces an object. It can be called from
anywhere but since you will probably not have a handle on a DOM node to
pass to it it will likely warn in environments other than the browser
* preloading APIS such as `preload`: These methods will execute the
preload across all renderers currently in scope. Since we resolve the
Request object on the server using AsyncLocalStorage or the current
function stack in practice only one renderer should act upon the
preload.

In addition to these changes the server rendering stub now just rexports
everything from `react-dom`. In a future minor we will add a warning
when using the stub and in the next major we will remove the stub
altogether
2024-04-24 08:50:32 -07:00
..
npm [react-dom] move all client code to react-dom/client (#28271) 2024-04-24 08:50:32 -07:00
src [react-dom] move all client code to react-dom/client (#28271) 2024-04-24 08:50:32 -07:00
client.js [react-dom] move all client code to react-dom/client (#28271) 2024-04-24 08:50:32 -07:00
index.js [react-dom] move all client code to react-dom/client (#28271) 2024-04-24 08:50:32 -07:00
package.json [react-dom] move all client code to react-dom/client (#28271) 2024-04-24 08:50:32 -07:00
profiling.js [react-dom] move all client code to react-dom/client (#28271) 2024-04-24 08:50:32 -07:00
README.md chore: update new docs links for react-dom (#26456) 2023-03-22 12:55:06 +01:00
server.browser.js Remove renderToStaticNodeStream (#28873) 2024-04-18 21:06:04 -07:00
server.bun.js Remove renderToStaticNodeStream (#28873) 2024-04-18 21:06:04 -07:00
server.edge.js Remove renderToStaticNodeStream (#28873) 2024-04-18 21:06:04 -07:00
server.js [Codemod] Update copyright header to Meta (#25315) 2022-10-18 11:19:24 -04:00
server.node.js Remove renderToStaticNodeStream (#28873) 2024-04-18 21:06:04 -07:00
static.browser.js [Fizz] Move /static build into /server builds (#27327) 2023-09-05 15:55:20 -04:00
static.edge.js [Fizz] Move /static build into /server builds (#27327) 2023-09-05 15:55:20 -04:00
static.js [Codemod] Update copyright header to Meta (#25315) 2022-10-18 11:19:24 -04:00
static.node.js [Fizz] Move /static build into /server builds (#27327) 2023-09-05 15:55:20 -04:00
test-utils.fb.js [TestUtils] Build limited test-utils (#28782) 2024-04-08 12:27:20 -07:00
test-utils.js [Codemod] Update copyright header to Meta (#25315) 2022-10-18 11:19:24 -04:00
unstable_server-external-runtime.js [ServerRenderer] Move fizz external runtime implementation to react-dom-bindings (#25617) 2022-11-03 11:15:29 -04:00
unstable_testing.experimental.js [react-dom] move all client code to react-dom/client (#28271) 2024-04-24 08:50:32 -07:00
unstable_testing.js [react-dom] move all client code to react-dom/client (#28271) 2024-04-24 08:50:32 -07:00

react-dom

This package serves as the entry point to the DOM and server renderers for React. It is intended to be paired with the generic React package, which is shipped as react to npm.

Installation

npm install react react-dom

Usage

In the browser

import { createRoot } from 'react-dom/client';

function App() {
  return <div>Hello World</div>;
}

const root = createRoot(document.getElementById('root'));
root.render(<App />);

On the server

import { renderToPipeableStream } from 'react-dom/server';

function App() {
  return <div>Hello World</div>;
}

function handleRequest(res) {
  // ... in your server handler ...
  const stream = renderToPipeableStream(<App />, {
    onShellReady() {
      res.statusCode = 200;
      res.setHeader('Content-type', 'text/html');
      stream.pipe(res);
    },
    // ...
  });
}

API

react-dom

See https://react.dev/reference/react-dom

react-dom/client

See https://react.dev/reference/react-dom/client

react-dom/server

See https://react.dev/reference/react-dom/server