From 7b62720daf05b92527f2f3e17d2d4a00ab38b5b4 Mon Sep 17 00:00:00 2001 From: Rick Hanlon Date: Thu, 17 Mar 2022 21:41:18 -0400 Subject: [PATCH 1/3] [18] Switch code samples to createRoot --- .../src/pages/learn/add-react-to-a-website.md | 19 +++---- content/docs/add-react-to-a-website.md | 7 +-- content/docs/addons-shallow-renderer.md | 2 +- content/docs/addons-test-utils.md | 6 +-- content/docs/components-and-props.md | 13 ++--- content/docs/concurrent-mode-adoption.md | 2 +- content/docs/forms.md | 4 +- content/docs/handling-events.md | 5 -- content/docs/hello-world.md | 7 +-- content/docs/hooks-faq.md | 4 +- content/docs/implementation-notes.md | 11 +++-- .../docs/integrating-with-other-libraries.md | 40 ++++++--------- content/docs/introducing-jsx.md | 10 ---- content/docs/lists-and-keys.md | 38 +++----------- content/docs/portals.md | 3 +- content/docs/react-without-es6.md | 6 +-- content/docs/react-without-jsx.md | 18 +++---- content/docs/reference-glossary.md | 5 +- ...nce-javascript-environment-requirements.md | 8 ++- content/docs/rendering-elements.md | 8 +-- content/docs/state-and-lifecycle.md | 49 ++++++------------- content/docs/testing-recipes.md | 2 +- content/docs/thinking-in-react.md | 2 +- content/docs/typechecking-with-proptypes.md | 6 +-- content/docs/web-components.md | 3 +- .../rendering-elements/render-an-element.js | 3 +- .../update-rendered-element.js | 4 +- 27 files changed, 101 insertions(+), 184 deletions(-) diff --git a/beta/src/pages/learn/add-react-to-a-website.md b/beta/src/pages/learn/add-react-to-a-website.md index 474514cb907..db8acb4df24 100644 --- a/beta/src/pages/learn/add-react-to-a-website.md +++ b/beta/src/pages/learn/add-react-to-a-website.md @@ -74,11 +74,12 @@ function LikeButton() { ### Step 4: Add your React Component to the page {/*step-4-add-your-react-component-to-the-page*/} -Lastly, add two lines to the bottom of **like_button.js**. These two lines of code find the `
` you added to your HTML in the first step and then display the "Like" button React component inside of it. +Lastly, add three lines to the bottom of **like_button.js**. These three lines of code find the `
` you added to your HTML in the first step, create a React app with it, and then display the "Like" button React component inside of it. ```js const domContainer = document.getElementById('component-goes-here'); -ReactDOM.render(React.createElement(LikeButton), domContainer); +const root = ReactDOM.createRoot(domContainer); +root.render(React.createElement(LikeButton)); ``` **Congratulations! You have just rendered your first React component to your website!** @@ -88,21 +89,21 @@ ReactDOM.render(React.createElement(LikeButton), domContainer); #### You can reuse components! {/*you-can-reuse-components*/} -You might want to display a React component in multiple places on the same HTML page. This is most useful while React-powered parts of the page are isolated from each other. You can do this by calling `ReactDOM.render()` multiple times with multiple container elements. +You might want to display a React component in multiple places on the same HTML page. This is most useful while React-powered parts of the page are isolated from each other. You can do this by calling `ReactDOM.createRoot()` multiple times with multiple container elements. 1. In **index.html**, add an additional container element `
`. 2. In **like_button.js**, add an additional `ReactDOM.render()` for the new container element: ```js {6,7,8,9} -ReactDOM.render( - React.createElement(LikeButton), +const root1 = ReactDOM.createRoot( document.getElementById('component-goes-here') ); +root1.render(React.createElement(LikeButton)); -ReactDOM.render( - React.createElement(LikeButton), +const root2 = ReactDOM.createRoot( document.getElementById('component-goes-here-too') ); +root2.render(React.createElement(LikeButton)); ``` Check out [an example that displays the "Like" button three times and passes some data to it](https://gist.github.com/rachelnabors/c0ea05cc33fbe75ad9bbf78e9044d7f8)! @@ -157,8 +158,8 @@ Now you can use JSX in any ` ``` diff --git a/content/docs/add-react-to-a-website.md b/content/docs/add-react-to-a-website.md index 50ce8475a3f..4aae1751fca 100644 --- a/content/docs/add-react-to-a-website.md +++ b/content/docs/add-react-to-a-website.md @@ -77,14 +77,15 @@ Open **[this starter code](https://gist.github.com/gaearon/0b180827c190fe4fd98b4 After **[the starter code](https://gist.github.com/gaearon/0b180827c190fe4fd98b4c7f570ea4a8/raw/b9157ce933c79a4559d2aa9ff3372668cce48de7/LikeButton.js)**, add two lines to the bottom of `like_button.js`: -```js{3,4} +```js{3,4,5} // ... the starter code you pasted ... const domContainer = document.querySelector('#like_button_container'); -ReactDOM.render(e(LikeButton), domContainer); +const root = ReactDOM.createRoot(domContainer); +root.render(e(LikeButton)); ``` -These two lines of code find the `
` we added to our HTML in the first step, and then display our "Like" button React component inside of it. +These three lines of code find the `
` we added to our HTML in the first step, create a React app with it, and then display our "Like" button React component inside of it. ### That's It! {#thats-it} diff --git a/content/docs/addons-shallow-renderer.md b/content/docs/addons-shallow-renderer.md index 7b7f29f3cda..324e61d7a29 100644 --- a/content/docs/addons-shallow-renderer.md +++ b/content/docs/addons-shallow-renderer.md @@ -59,7 +59,7 @@ Shallow testing currently has some limitations, namely not supporting refs. You can think of the shallowRenderer as a "place" to render the component you're testing, and from which you can extract the component's output. -`shallowRenderer.render()` is similar to [`ReactDOM.render()`](/docs/react-dom.html#render) but it doesn't require DOM and only renders a single level deep. This means you can test components isolated from how their children are implemented. +`shallowRenderer.render()` is similar to [`root.render()`](/docs/react-dom-client.html#createroot) but it doesn't require DOM and only renders a single level deep. This means you can test components isolated from how their children are implemented. ### `shallowRenderer.getRenderOutput()` {#shallowrenderergetrenderoutput} diff --git a/content/docs/addons-test-utils.md b/content/docs/addons-test-utils.md index 08f9a5f0945..49075a08785 100644 --- a/content/docs/addons-test-utils.md +++ b/content/docs/addons-test-utils.md @@ -89,7 +89,7 @@ Here is how we can test it: ```js{3,20-22,29-31} import React from 'react'; -import ReactDOM from 'react-dom'; +import ReactDOM from 'react-dom/client'; import { act } from 'react-dom/test-utils'; import Counter from './Counter'; @@ -108,7 +108,7 @@ afterEach(() => { it('can render and update a counter', () => { // Test first render and componentDidMount act(() => { - ReactDOM.render(, container); + ReactDOM.createRoot(container).render(); }); const button = container.querySelector('button'); const label = container.querySelector('p'); @@ -304,7 +304,7 @@ Render a React element into a detached DOM node in the document. **This function ```js const domContainer = document.createElement('div'); -ReactDOM.render(element, domContainer); +ReactDOM.createRoot(domContainer).render(element); ``` > Note: diff --git a/content/docs/components-and-props.md b/content/docs/components-and-props.md index cb3b7edad85..3f6b1f2f42a 100644 --- a/content/docs/components-and-props.md +++ b/content/docs/components-and-props.md @@ -70,17 +70,15 @@ function Welcome(props) { } const element = ; -ReactDOM.render( - element, - document.getElementById('root') -); +const root = ReactDOM.createRoot(document.getElementById('root')); +root.render(element); ``` **[Try it on CodePen](https://codepen.io/gaearon/pen/YGYmEG?editors=1010)** Let's recap what happens in this example: -1. We call `ReactDOM.render()` with the `` element. +1. We call `root.render()` with the `` element. 2. React calls the `Welcome` component with `{name: 'Sara'}` as the props. 3. Our `Welcome` component returns a `

Hello, Sara

` element as the result. 4. React DOM efficiently updates the DOM to match `

Hello, Sara

`. @@ -111,11 +109,6 @@ function App() {
); } - -ReactDOM.render( - , - document.getElementById('root') -); ``` **[Try it on CodePen](https://codepen.io/gaearon/pen/KgQKPr?editors=1010)** diff --git a/content/docs/concurrent-mode-adoption.md b/content/docs/concurrent-mode-adoption.md index 54806d60b5d..5cbc93cc6ef 100644 --- a/content/docs/concurrent-mode-adoption.md +++ b/content/docs/concurrent-mode-adoption.md @@ -74,7 +74,7 @@ import ReactDOM from 'react-dom'; // // You can opt into Concurrent Mode by writing: -ReactDOM.unstable_createRoot( +ReactDOM.createRoot( document.getElementById('root') ).render(); ``` diff --git a/content/docs/forms.md b/content/docs/forms.md index c43a72a3bf9..08344f4b993 100644 --- a/content/docs/forms.md +++ b/content/docs/forms.md @@ -275,10 +275,10 @@ Specifying the `value` prop on a [controlled component](/docs/forms.html#control The following code demonstrates this. (The input is locked at first but becomes editable after a short delay.) ```javascript -ReactDOM.render(, mountNode); +ReactDOM.createRoot(mountNode).render(); setTimeout(function() { - ReactDOM.render(, mountNode); + ReactDOM.createRoot(mountNode).render(); }, 1000); ``` diff --git a/content/docs/handling-events.md b/content/docs/handling-events.md index 5a746ee612b..a3b56b58945 100644 --- a/content/docs/handling-events.md +++ b/content/docs/handling-events.md @@ -84,11 +84,6 @@ class Toggle extends React.Component { ); } } - -ReactDOM.render( - , - document.getElementById('root') -); ``` [**Try it on CodePen**](https://codepen.io/gaearon/pen/xEmzGg?editors=0010) diff --git a/content/docs/hello-world.md b/content/docs/hello-world.md index 79b208d9ead..2addca93265 100644 --- a/content/docs/hello-world.md +++ b/content/docs/hello-world.md @@ -8,11 +8,8 @@ next: introducing-jsx.html The smallest React example looks like this: -```js -ReactDOM.render( -

Hello, world!

, - document.getElementById('root') -); +```jsx +

Hello, world!

``` It displays a heading saying "Hello, world!" on the page. diff --git a/content/docs/hooks-faq.md b/content/docs/hooks-faq.md index 81820652641..8976b0a0a21 100644 --- a/content/docs/hooks-faq.md +++ b/content/docs/hooks-faq.md @@ -148,7 +148,7 @@ We'll test it using React DOM. To make sure that the behavior matches what happe ```js{3,20-22,29-31} import React from 'react'; -import ReactDOM from 'react-dom'; +import ReactDOM from 'react-dom/client'; import { act } from 'react-dom/test-utils'; import Counter from './Counter'; @@ -167,7 +167,7 @@ afterEach(() => { it('can render and update a counter', () => { // Test first render and effect act(() => { - ReactDOM.render(, container); + ReactDOM.createRoot(container).render(); }); const button = container.querySelector('button'); const label = container.querySelector('p'); diff --git a/content/docs/implementation-notes.md b/content/docs/implementation-notes.md index b345f7d5d06..ff67f062f18 100644 --- a/content/docs/implementation-notes.md +++ b/content/docs/implementation-notes.md @@ -32,10 +32,11 @@ The reconciler itself doesn't have a public API. [Renderers](/docs/codebase-over Let's consider the first time you mount a component: ```js -ReactDOM.render(, rootEl); +const root = ReactDOM.createRoot(rootEl); +root.render(); ``` -React DOM will pass `` along to the reconciler. Remember that `` is a React element, that is, a description of *what* to render. You can think about it as a plain object: +`root.render` will pass `` along to the reconciler. Remember that `` is a React element, that is, a description of *what* to render. You can think about it as a plain object: ```js console.log(); @@ -236,9 +237,9 @@ This is working but still far from how the reconciler is really implemented. The The key feature of React is that you can re-render everything, and it won't recreate the DOM or reset the state: ```js -ReactDOM.render(, rootEl); +root.render(); // Should reuse the existing DOM: -ReactDOM.render(, rootEl); +root.render(); ``` However, our implementation above only knows how to mount the initial tree. It can't perform updates on it because it doesn't store all the necessary information, such as all the `publicInstance`s, or which DOM `node`s correspond to which components. @@ -412,7 +413,7 @@ If you're struggling to imagine how an internal instance tree is structured in m React DevTools tree -To complete this refactoring, we will introduce a function that mounts a complete tree into a container node, just like `ReactDOM.render()`. It returns a public instance, also like `ReactDOM.render()`: +To complete this refactoring, we will introduce a function that mounts a complete tree into a container node and a public instance: ```js function mountTree(element, containerNode) { diff --git a/content/docs/integrating-with-other-libraries.md b/content/docs/integrating-with-other-libraries.md index 5bc8b257034..83fa1768dcf 100644 --- a/content/docs/integrating-with-other-libraries.md +++ b/content/docs/integrating-with-other-libraries.md @@ -190,9 +190,9 @@ class Chosen extends React.Component { ## Integrating with Other View Libraries {#integrating-with-other-view-libraries} -React can be embedded into other applications thanks to the flexibility of [`ReactDOM.render()`](/docs/react-dom.html#render). +React can be embedded into other applications thanks to the flexibility of [`createRoot()`](/docs/react-dom-client.html#createRoot). -Although React is commonly used at startup to load a single root React component into the DOM, `ReactDOM.render()` can also be called multiple times for independent parts of the UI which can be as small as a button, or as large as an app. +Although React is commonly used at startup to load a single root React component into the DOM, `root.render()` can also be called multiple times for independent parts of the UI which can be as small as a button, or as large as an app. In fact, this is exactly how React is used at Facebook. This lets us write applications in React piece by piece, and combine them with our existing server-generated templates and other client-side code. @@ -216,15 +216,9 @@ function Button() { return ; } -ReactDOM.render( -