Use React 18's New Root API Alongside the Legacy Root API

Michael Chan
InstructorMichael Chan
Share this video with your friends

Social Share Links

Send Tweet
Published 3 years ago
Updated 3 years ago

Utilize both React root APIs to migrate your app. We can render a portion of our app using the legacy root API (v17 compatible) and a second using the new root API (v18 exclusive). This option makes it possible to migrate portions of your app at a time.

Instructor: [0:00] One of the nice outcomes of ReactDOM shipping with two root APIs is that we can render to different parts of our page using the different root APIs. If part of our app isn't quite stable yet, with the new root API, we can migrate those selectively. Let's render to both of these roots.

[0:19] For Root 1, we'll use the new React root API, ReactDOM.createRoot from root1.render(<App />). Hit refresh in our browser. This takes a root API, so we can pass that along to keep track of these differences. To Root 2, we'll use the legacy ReactDOM.render() API, providing the component as the first argument.

[0:50] Let's save, re-render our page, update this to Root 17, not that it is 17 but that it behaves like 17. There we go.

[1:03] Because we added this one second, we can confirm through this warning message that, indeed, it is using the ReactDOM 17 legacy root API, rendering everything in that root as if it were version 17.