You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/blog/2024/04/25/react-19-upgrade-guide.md
+16-16
Original file line number
Diff line number
Diff line change
@@ -36,10 +36,10 @@ For a list of changes in 18.3 see the [Release Notes](https://github.com/faceboo
36
36
In this post, we will guide you through the steps for upgrading libraries to React 19 beta:
37
37
38
38
-[Installing](#installing)
39
-
-[Breaking Changes](#breaking-changes)
40
-
-[New Deprecations](#new-deprecations)
41
-
-[Notable Changes](#notable-changes)
42
-
-[TypeScript Changes](#typescript-changes)
39
+
-[Breaking changes](#breaking-changes)
40
+
-[New deprecations](#new-deprecations)
41
+
-[Notable changes](#notable-changes)
42
+
-[TypeScript changes](#typescript-changes)
43
43
-[Changelog](#changelog)
44
44
45
45
If you'd like to help us test React 19, follow the steps in this upgrade guide and [report any issues](https://github.com/facebook/react/issues/new?assignees=&labels=React+19&projects=&template=19.md&title=%5BReact+19%5D) you encounter. For a list of new features added to React 19 beta, see the [React 19 release post](/blog/2024/04/25/react-19).
@@ -92,10 +92,10 @@ If you're using TypeScript, you also need to update the types. Once React 19 is
92
92
}
93
93
```
94
94
95
-
We're also including a codemod for the most common replacements. See [Removed TypeScript Types](#removed-deprecated-typescript-types) below.
95
+
We're also including a codemod for the most common replacements. See [TypeScript changes](#typescript-changes) below.
96
96
97
97
98
-
## Breaking Changes {/*breaking-changes*/}
98
+
## Breaking changes {/*breaking-changes*/}
99
99
100
100
### Errors in render are not re-thrown {/*errors-in-render-are-not-re-thrown*/}
101
101
@@ -163,7 +163,7 @@ Legacy Context was only available in class components using the APIs `contextTyp
163
163
164
164
If you're still using Legacy Context in class components, you'll need to migrate to the new `contextType` API:
165
165
166
-
```js
166
+
```js {5-11,19-21}
167
167
// Before
168
168
importPropTypesfrom'prop-types';
169
169
@@ -192,7 +192,7 @@ class Child extends React.Component {
192
192
}
193
193
```
194
194
195
-
```js
195
+
```js {2,7,9,15}
196
196
// After
197
197
constFooContext=React.createContext();
198
198
@@ -222,7 +222,7 @@ Class components supported string refs before being replaced by ref callbacks du
222
222
223
223
If you're still using string refs in class components, you'll need to migrate to ref callbacks:
224
224
225
-
```js
225
+
```js {4,8}
226
226
// Before
227
227
classMyComponentextendsReact.Component {
228
228
componentDidMount() {
@@ -235,7 +235,7 @@ class MyComponent extends React.Component {
235
235
}
236
236
```
237
237
238
-
```js
238
+
```js {4,8}
239
239
// After
240
240
classMyComponentextendsReact.Component {
241
241
componentDidMount() {
@@ -414,7 +414,7 @@ function AutoselectingInput() {
@@ -440,7 +440,7 @@ The test renderer was created before there were more viable testing strategies a
440
440
441
441
In React 19, `react-test-renderer` logs a deprecation warning, and has switched to concurrent rendering. We recommend migrating your tests to [@testing-library/react](https://testing-library.com/docs/react-testing-library/intro/) or [@testing-library/react-native](https://callstack.github.io/react-native-testing-library/docs/getting-started) for a modern and well supported testing experience.
Copy file name to clipboardExpand all lines: src/content/blog/2024/04/25/react-19.md
+32-38
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ In our [React 19 Beta Upgrade Guide](/blog/2024/04/25/react-19-upgrade-guide), w
22
22
23
23
-[What's new in React 19](#whats-new-in-react-19)
24
24
-[Improvements in React 19](#improvements-in-react-19)
25
-
-[How to Upgrade](#how-to-upgrade)
25
+
-[How to upgrade](#how-to-upgrade)
26
26
27
27
For a list of breaking changes, see the [Upgrade Guide](/blog/2024/04/25/react-19-upgrade-guide).
28
28
@@ -115,7 +115,7 @@ Actions automatically manage submitting data for you:
115
115
116
116
</Note>
117
117
118
-
Building on top of Actions, we're also introducing [`<form>` Actions](#form-actions) to manage forms automatically, [`useOptimistic`](#new-hook-optimistic-updates) to manage optimistic updates, and new hooks [`useActionState`](#new-hook-useactionstate), [`useFormStatus`](#new-hook-useformstatus)hooks to support the common cases for Actions and Forms.
118
+
Building on top of Actions, we're also introducing [`<form>` Actions](#form-actions) to manage forms automatically, [`useOptimistic`](#new-hook-optimistic-updates) to manage optimistic updates, and new hooks [`useActionState`](#new-hook-useactionstate), [`useFormStatus`](#new-hook-useformstatus) to support the common cases for Actions and Forms.
119
119
120
120
In React 19, the above example can be simplified to:
121
121
@@ -144,7 +144,7 @@ function ChangeName({ name, setName }) {
144
144
145
145
In the next section, we'll break down each of the new Action features in React 19.
146
146
147
-
### New Hook: `useActionState` {/*new-hook-useactionstate*/}
147
+
### New hook: `useActionState` {/*new-hook-useactionstate*/}
148
148
149
149
To make the common cases easier for Actions, we've added a new hook called `useActionState`:
150
150
@@ -185,7 +185,7 @@ When a `<form>` Action succeeds, React will automatically reset the form for unc
185
185
186
186
For more information, see the docs for [`<form>`](/reference/react-dom/components/form), [`<input>`](/reference/react-dom/components/input), and [`<button>`](/reference/react-dom/components/button).
187
187
188
-
### New Hook: `useFormStatus` {/*new-hook-useformstatus*/}
188
+
### New hook: `useFormStatus` {/*new-hook-useformstatus*/}
189
189
190
190
In design systems, it's common to write design components that need access to information about the `<form>` they're in, without drilling props down to the component. This can be done via Context, but to make the common case easier, we've added a new hook `useFormStatus`:
191
191
@@ -200,7 +200,7 @@ function DesignButton() {
200
200
201
201
For more information, see the docs for [`useFormStatus`](/reference/react-dom/hooks/useFormStatus).
202
202
203
-
### New Hook: `useOptimistic` {/*new-hook-optimistic-updates*/}
203
+
### New hook: `useOptimistic` {/*new-hook-optimistic-updates*/}
204
204
205
205
Another common UI pattern when performing a data mutation is to show the final state optimistically while the async request is underway. In React 19, we're adding a new hook called `useOptimistic` to make this easier:
206
206
@@ -272,7 +272,7 @@ To fix, you need to pass a promise from a suspense powered library or framework
272
272
273
273
</Note>
274
274
275
-
You can also read context with `use`, allowing you to react Context conditionally:
275
+
You can also read context with `use`, allowing you to read Context conditionally:
276
276
277
277
```js {1,8,10}
278
278
import {use} from'react';
@@ -364,7 +364,7 @@ New function components will no longer need `forwardRef`, and we will be publish
364
364
365
365
</Note>
366
366
367
-
### Diffs for Hydration Errors {/*diffs-for-hydration-errors*/}
367
+
### Diffs for hydration errors {/*diffs-for-hydration-errors*/}
368
368
369
369
We also improved error reporting for hydration errors. For example, instead of logging multiple errors in DEV without any information about the mismatch:
370
370
@@ -457,7 +457,7 @@ New Context providers can use `<Context>` and we will be publishing a codemod to
457
457
458
458
We now support returning a cleanup function from `ref` callbacks:
459
459
460
-
```js {7-11}
460
+
```js {7-9}
461
461
<input
462
462
ref={(ref) => {
463
463
// ref created
@@ -481,21 +481,19 @@ In future versions, we will deprecate calling refs with `null` when unmounting c
481
481
482
482
</Note>
483
483
484
-
Due to the introduction of ref cleanup functions, returning anything else from a `ref` callback will now be rejected by TypeScript.
484
+
Due to the introduction of ref cleanup functions, returning anything else from a `ref` callback will now be rejected by TypeScript. The fix is usually to stop using implicit returns, for example:
485
485
486
-
The fix is usually to stop using implicit returns, for example:
The original code returned the instance of the `HTMLDivElement` and TypeScript wouldn't know if this was _supposed_ to be a cleanup function or if you didn't want to return a cleanup function.
494
492
495
493
You can codemod this pattern with [`no-implicit-ref-callback-return
### `useDeferredValue`inital value {/*use-deferred-value-initial-value*/}
496
+
### `useDeferredValue`initial value {/*use-deferred-value-initial-value*/}
499
497
500
498
We've added an `initalValue` option to `useDeferredValue`:
501
499
@@ -521,7 +519,7 @@ In HTML, document metadata tags like `<title>`, `<link>`, and `<meta>` are reser
521
519
522
520
In React 19, we're adding support for rendering document metadata tags in components natively:
523
521
524
-
```js {5,6}
522
+
```js {5-8}
525
523
function BlogPost({post}) {
526
524
return (
527
525
<article>
@@ -556,14 +554,14 @@ Stylesheets, both externally linked (`<link rel="stylesheet" href="...">`) and i
556
554
557
555
In React 19, we're addressing this complexity and providing even deeper integration into Concurrent Rendering on the Client and Streaming Rendering on the Server with built in support for stylesheets. If you tell React the `precedence` of your stylesheet it will manage the insertion order of the stylesheet in the DOM and ensure that the stylesheet (if external) is loaded before revealing content that depends on those style rules.
<link rel="stylesheet" href="baz" precedence="default" /> <-- will be inserted between foo & bar
577
575
</div>
578
576
)
@@ -581,10 +579,9 @@ function ComponentTwo() {
581
579
582
580
During Server Side Rendering React will include the stylesheet in the `<head>`, which ensures that the browser will not paint until it has loaded. If the stylesheet is discovered late after we've already started streaming, React will ensure that the stylesheet is inserted into the `<head>` on the client before revealing the content of a Suspense boundary that depends on that stylesheet.
583
581
584
-
During Client Side Rendering React will wait for newly rendered stylesheets to load before committing the render.
582
+
During Client Side Rendering React will wait for newly rendered stylesheets to load before committing the render.If you render this component from multiple places within your application React will only include the stylesheet once in the document:
585
583
586
-
If you render this component from multiple places within your application React will only include the stylesheet once in the document:
587
-
```js
584
+
```js {5}
588
585
function App() {
589
586
return <>
590
587
<ComponentOne />
@@ -596,7 +593,7 @@ function App() {
596
593
597
594
For users accustomed to loading stylesheets manually this is an opportunity to locate those stylesheets alongside the components that depend on them allowing for better local reasoning and an easier time ensuring you only load the stylesheets that you actually depend on.
598
595
599
-
style libraries and style integrations with bundlers can also adopt thisnewcapability so even if you don't directly render your own stylesheets, you can still benefit as your tools are upgraded to use this feature.
596
+
Style libraries and style integrations with bundlers can also adopt thisnewcapability so even if you don't directly render your own stylesheets, you can still benefit as your tools are upgraded to use this feature.
600
597
601
598
For more details, read the docs for [`<link>`](/reference/react-dom/components/link) and [`<style>`](/reference/react-dom/components/style).
602
599
@@ -606,7 +603,7 @@ In HTML normal scripts (`<script src="...">`) and deferred scripts (`<script def
606
603
607
604
In React 19 we've included better support forasync scripts by allowing you to render them anywhere in your component tree, inside the components that actually depend on the script, without having to manage relocating and deduplicating script instances.
608
605
609
-
```js
606
+
```js {4,15}
610
607
function MyComponent() {
611
608
return (
612
609
<div>
@@ -627,9 +624,9 @@ function App() {
627
624
}
628
625
```
629
626
630
-
In all rendering environments async scripts will be deduplicated so that React will only load and execute the script once even if it is rendered by multiple different components.
627
+
In all rendering environments,async scripts will be deduplicated so that React will only load and execute the script once even if it is rendered by multiple different components.
631
628
632
-
In Server Side Rendering async scripts will be included in the `<head>` and prioritized behind more critical resources that block paint such as stylesheets, fonts, and image preloads.
629
+
In Server Side Rendering,async scripts will be included in the `<head>` and prioritized behind more critical resources that block paint such as stylesheets, fonts, and image preloads.
633
630
634
631
For more details, read the docs for [`<script>`](/reference/react-dom/components/script).
635
632
@@ -666,9 +663,7 @@ function MyComponent() {
666
663
</html>
667
664
```
668
665
669
-
These APIs can be used to make initial page loads optimized for instance by moving discovery of additional resources like fonts out of stylesheet loading
670
-
671
-
These APIs can be used to make client updates faster for instance by prefetching a list of resources used by an anticipated navigation and then eagerly preloading those resources on click or even on hover.
666
+
These APIs can be used to optimize initial page loads by moving discovery of additional resources like fonts out of stylesheet loading. They can also make client updates faster by prefetching a list of resources used by an anticipated navigation and then eagerly preloading those resources on click or even on hover.
672
667
673
668
For more details see [Resource Preloading APIs](/reference/react-dom#resource-preloading-apis).
674
669
@@ -678,9 +673,9 @@ We've improved hydration to account for third-party scripts and browser extensio
678
673
679
674
When hydrating, if an element that renders on the client doesn't match the element found in the HTML from the server, React will force a client re-render to fix up the content. Previously, if an element was inserted by third-party scripts or browser extensions, it would trigger a mismatch error and client render.
680
675
681
-
In React 19 unexpected tags in the `<head>` and `<body>` will be skipped over, avoiding the mismatch errors. If React needs to re-render the entire document due to an unrelated hydration mismatch, it will leave in place stylesheets inserted by third-party scripts and browser extensions.
676
+
In React 19, unexpected tags in the `<head>` and `<body>` will be skipped over, avoiding the mismatch errors. If React needs to re-render the entire document due to an unrelated hydration mismatch, it will leave in place stylesheets inserted by third-party scripts and browser extensions.
682
677
683
-
### Better Error Reporting {/*error-handling*/}
678
+
### Better error reporting {/*error-handling*/}
684
679
685
680
We improved error handling in React 19 to remove duplication and provide options for handling caught and uncaught errors. For example, when there's an error in render caught by an Error Boundary, previously React would throw the error twice (once for the original error, then again after failing to automatically recover), and then call `console.error` with info about where the error occurred.
686
681
@@ -748,20 +743,19 @@ Additionally, we've added two new root options to complement `onRecoverableError
748
743
749
744
For more info and examples, see the docs for [`createRoot`](/reference/react-dom/client/createRoot) and [`hydrateRoot`](/reference/react-dom/client/hydrateRoot).
750
745
751
-
### Custom Element Support {/*support-for-web-components*/}
752
-
753
-
Custom Elements have been part of the web platform for a long while now, however using them with React has been impractical because React has always treated unrecognized props as attributes rather than properties.
746
+
### Support for Custom Elements {/*support-for-custom-elements*/}
754
747
755
-
React 19 adds support for custom elements and passes all tests on [Custom Elements Everywhere](https://custom-elements-everywhere.com/).
748
+
React 19 adds full support for custom elements and passes all tests on [Custom Elements Everywhere](https://custom-elements-everywhere.com/).
756
749
757
-
During Server Side Rendering props passed to a custom element will render as attributes if their type is a `"string"`, `"number"`, or the value is `true`. Props with type `"object"`, `"symbol"`, `"function"`, or value `false` will be omitted.
750
+
In past versions, using Custom Elements in React has been difficult because React treated unrecognized props as attributes rather than properties. In React 19, we've added support for properties that works on the client and during SSR with the following strategy:
758
751
759
-
During Client Side Rendering props that match a property on the Custom Element instance will be assigned as properties, otherwise they will be assigned as attributes.
752
+
- **Server Side Rendering**: props passed to a custom element will render as attributes if their type is a primitive value like `string`, `number`, or the value is `true`. Props with non-primitive types like `object`, `symbol`, `function`, or value `false` will be omitted.
753
+
- **Client Side Rendering**: props that match a property on the Custom Element instance will be assigned as properties, otherwise they will be assigned as attributes.
760
754
761
755
Thanks to [Joey Arhar](https://github.com/josepharhar) for driving the design and implementation of Custom Element support in React.
0 commit comments