2015-06-12 05:19:52 +08:00
# `react-dom`
2018-09-06 22:22:10 +08:00
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.
2015-06-12 05:19:52 +08:00
## Installation
```sh
npm install react react-dom
```
## Usage
### In the browser
```js
2022-04-07 20:46:36 +08:00
import { createRoot } from 'react-dom/client';
2015-06-12 05:19:52 +08:00
2022-04-07 09:35:01 +08:00
function App() {
2021-09-07 04:23:13 +08:00
return < div > Hello World< / div > ;
2015-06-12 05:19:52 +08:00
}
2022-04-07 09:35:01 +08:00
const root = createRoot(document.getElementById('root'));
root.render(< App / > );
2015-06-12 05:19:52 +08:00
```
### On the server
```js
2022-04-07 09:35:01 +08:00
import { renderToPipeableStream } from 'react-dom/server';
2015-06-12 05:19:52 +08:00
2022-04-07 09:35:01 +08:00
function App() {
2021-09-07 04:23:13 +08:00
return < div > Hello World< / div > ;
2015-06-12 05:19:52 +08:00
}
2022-04-07 09:35:01 +08:00
function handleRequest(res) {
// ... in your server handler ...
const stream = renderToPipeableStream(< App / > , {
onShellReady() {
res.statusCode = 200;
res.setHeader('Content-type', 'text/html');
stream.pipe(res);
},
// ...
});
}
2015-06-12 05:19:52 +08:00
```
## API
### `react-dom`
2022-04-07 09:35:01 +08:00
See https://reactjs.org/docs/react-dom.html
### `react-dom/client`
See https://reactjs.org/docs/react-dom-client.html
2015-06-12 05:19:52 +08:00
### `react-dom/server`
2022-04-07 09:35:01 +08:00
See https://reactjs.org/docs/react-dom-server.html