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/01/react-19.md
+54-36Lines changed: 54 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,9 +35,7 @@ _Note for React Native users: React 19 will ship a future version of React Nativ
35
35
36
36
<Note>
37
37
38
-
React Conf 2024 is scheduled for May 15–16 in Henderson, Nevada!
39
-
40
-
For more see [the React Conf website](https://conf.react.dev).
38
+
Stream [React Conf 2024]((https://conf.react.dev)) live May 15–16!
41
39
42
40
</Note>
43
41
@@ -47,7 +45,9 @@ For more see [the React Conf website](https://conf.react.dev).
47
45
48
46
### Actions {/*actions*/}
49
47
50
-
A common use case in React apps is to perform a data mutation and then update state in response. For example, when a user submits a form to change their name, you will make an API request, and then handle the response. Since this is an async request, you need to handle the pending state in a separate useState call:
48
+
A common use case in React apps is to perform a data mutation and then update state in response. For example, when a user submits a form to change their name, you will make an API request, and then handle the response. In the past, you would need to handle pending states, errors, optimistic updates, and sequential requests manually.
49
+
50
+
For example, you could handle the pending state in `useState`:
In React 19, we added support for using async functions in transitions:
71
+
In React 19, we're adding support for using async functions in transitions to handle pending states, errors, forms, and optimistic updates automatically.
72
+
73
+
For example, you can use `useTransition` to handle the pending state for you:
By convention, functions that use async transitions are called "Actions". Actions will immediately set the `isPending` state to true, make the async request(s), and render any state updates as transitions. This allows you to keep the current UI responsive and interactive while the data is changing.
93
94
94
-
For more information, see the docs for [`useTransition`](/reference/react/useTransition).
95
+
The async transition will immediately set the `isPending` state to true, make the async request(s), and render any state updates as transitions. This allows you to keep the current UI responsive and interactive while the data is changing.
96
+
97
+
<Note>
98
+
99
+
#### By convention, functions that use async transitions are called "Actions". {/*by-convention-functions-that-use-async-transitions-are-called-actions*/}
100
+
101
+
Actions automatically manage submitting data for you:
102
+
103
+
-**Pending state**: Actions provide a pending state that starts at the beginning of a request and automatically resets when the final state update is committed.
104
+
-**Optimistic updates**: Actions support the new [`useOptimistic`](#new-feature-optimistic-updates) hook to handle optimistically showing the user the final state while requests are submitting.
105
+
-**Error Handling**: Actions provide error handling so you can and display Error Boundaries when a request fails, and revert optimistic updates to their original value automatically.
106
+
-**Forms**: Actions support new `action` prop for `<form>` elements called [Form Actions](#form-actions). This means form submissions use Actions by default, and reset automatically after submission.
107
+
108
+
</Note>
109
+
110
+
Async transitions are the raw primitive that power Actions, and you can always drop down to `useTransition`, `useState`, and `useOptimisitc` to create your own custom behavior. We're also introducing the [`useActionState`](#new-hook-useactionstate) and [`useFormStatus`](#new-hook-useformstatus) hooks to support the common cases for Actions and Forms.
111
+
112
+
For more information, see the docs for [`useTransition`](/reference/react/useTransition) and the next sections.
95
113
96
114
### New Hook: `useActionState` {/*new-hook-useactionstate*/}
97
115
98
-
You can always create an Action by dropping down to `useTransition`, but to make the common cases easier we've added a new hook called `useActionState`:
116
+
To make the common cases easier for Actions, we've added a new hook called `useActionState`:
`useActionState` accepts a function (the "Action"), and returns a new Action to call. This works because Actions compose. When the new Action is called, `useActionState` will return the last result of the Action as `data`, and the pending state of the Action as `pending`.
130
+
`useActionState` accepts a function (the "Action"), and returns a wrapped Action to call. This works because Actions compose. When the wrapped Action is called, `useActionState` will return the last result of the Action as `data`, and the pending state of the Action as `pending`.
113
131
114
132
<Note>
115
133
@@ -168,15 +186,15 @@ Another common UI pattern when performing a data mutation is to show the final s
@@ -201,10 +219,10 @@ For example, you can read a promise with `use`, and React will Suspend until the
201
219
```js {1,6}
202
220
import {use} from'react';
203
221
204
-
functionComments({loadComments}) {
222
+
functionComments({commentsPromise}) {
205
223
// NOTE: this will resume the promise from the server.
206
224
// It will suspend until the data is available.
207
-
constcomments=use(loadComments);
225
+
constcomments=use(commentsPromise);
208
226
returncomments.map(commment=><p>{comment}</p>);
209
227
}
210
228
```
@@ -241,7 +259,7 @@ TODO: we can't yet
241
259
242
260
Server Components are a new option that allows rendering components ahead of time, before bundling, in an environment separate from your application (the "server"). They can run once at build time, or can be run for each request to a web server.
243
261
244
-
Today we're releasing React Server Components as semver stable in React 19. This means libraries that ship Server Components and Server Action can target React 19 as a peer dependency for use in frameworks that support the [Full-stack React Architecture](/learn/start-a-new-react-project#which-features-make-up-the-react-teams-full-stack-architecture-vision).
262
+
Today we're releasing React Server Components as semver stable in React 19. This means libraries that ship Server Components and Server Actions can target React 19 as a peer dependency for use in frameworks that support the [Full-stack React Architecture](/learn/start-a-new-react-project#which-features-make-up-the-react-teams-full-stack-architecture-vision).
245
263
246
264
<DeepDive>
247
265
@@ -525,13 +543,13 @@ async function Page({id}) {
525
543
// Will suspend the Server Component.
526
544
constnote=awaitdb.notes.get(id);
527
545
528
-
// NOTE: not awaited, will resume and suspend on the client.
529
-
constloadComments=db.comments.get(note.id);
546
+
// NOTE: not awaited, will start here and await on the client.
547
+
constcommentsPromise=db.comments.get(note.id);
530
548
return (
531
549
<div>
532
550
{note}
533
551
<Suspense fallback={<p>Loading Comments...</p>}>
534
-
<Comments loadComments={loadComments} />
552
+
<Comments commentsPromise={commentsPromise} />
535
553
</Suspense>
536
554
</div>
537
555
);
@@ -543,10 +561,10 @@ async function Page({id}) {
543
561
"use client";
544
562
import {use} from'react';
545
563
546
-
functionComments({loadComments}) {
564
+
functionComments({commentsPromise}) {
547
565
// NOTE: this will resume the promise from the server.
548
566
// It will suspend until the data is available.
549
-
constcomments=use(loadComments);
567
+
constcomments=use(commentsPromise);
550
568
returncomments.map(commment=><p>{comment}</p>);
551
569
}
552
570
```
@@ -575,30 +593,30 @@ TODO
575
593
576
594
Server Components can define Server Actions with the `"use server"` directive:
When React renders the `EmptyNote` Server Component, it will create a reference to the `emptyNoteAction` function, and pass that reference to the `Button` Client Component. When the button is clicked, React will send a request to the server to execute the `emptyNoteAction` function with the reference provided:
612
+
When React renders the `EmptyNote` Server Component, it will create a reference to the `createNoteAction` function, and pass that reference to the `Button` Client Component. When the button is clicked, React will send a request to the server to execute the `createNoteAction` function with the reference provided:
@@ -610,25 +628,25 @@ For more, see the docs for [`"use server"`](/reference/react/use-server).
610
628
611
629
Client Components can import Server Actions from files that use the `"use server"` directive:
612
630
613
-
```js [[1, 3, "emptyNoteAction"]]
631
+
```js [[1, 3, "createNoteAction"]]
614
632
"use server";
615
633
616
-
exportasyncfunctionemptyNoteAction() {
634
+
exportasyncfunctioncreateNoteAction() {
617
635
awaitdb.notes.create();
618
636
}
619
637
620
638
```
621
639
622
-
When the bundler builds the `EmptyNote` Client Component, it will create a reference to the `emptyNoteAction` function in the bundle. When the `button` is clicked, React will send a request to the server to execute the `emptyNoteAction` function using the reference provided:
640
+
When the bundler builds the `EmptyNote` Client Component, it will create a reference to the `createNoteAction` function in the bundle. When the `button` is clicked, React will send a request to the server to execute the `createNoteAction` function using the reference provided:
// Then a re-render is scheduled with the deferredValue.
@@ -862,7 +880,7 @@ function Search({deferredValue}) {
862
880
}
863
881
````
864
882
865
-
When <CodeStep step={3}>initialValue</CodeStep> is provided, React will return it as the <CodeStep step={2}>value</CodeStep>for the initial render of the component, and scheduled a re-render in the background with the <CodeStep step={1}>deferredValue</CodeStep> returned.
883
+
When <CodeStep step={2}>initialValue</CodeStep> is provided, `useDeferredValue` will return it as `value`for the initial render of the component, and scheduled a re-render in the background with the <CodeStep step={1}>deferredValue</CodeStep> returned.
866
884
867
885
For more, see [`useDeferredValue`](/reference/react/useDeferredValue).
868
886
@@ -917,7 +935,7 @@ We've improved hydration to account for third-party scripts and browser extensio
917
935
918
936
### Better Error Reporting {/*error-handling*/}
919
937
920
-
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.
938
+
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.
921
939
922
940
This resulted in three errors for every caught error:
0 commit comments