diff --git a/src/content/blog/2024/04/01/react-19.md b/src/content/blog/2024/04/01/react-19.md index 15ed69cb8..0741598b2 100644 --- a/src/content/blog/2024/04/01/react-19.md +++ b/src/content/blog/2024/04/01/react-19.md @@ -2,16 +2,33 @@ title: "React 19 Beta" --- -April 1, 2024 by [The React Team](/community/team) +April 1, 2024, by [The React Team](/community/team) --- +**TODO** +- Content + - [ ] Finish RSC and RSA sections and examples + - [ ] Finish other incomplete sections + - [ ] Write Deep Dives + - [ ] Review changelog and add any missing sections + - [ ] Finish inline todos +- Polish + - [ ] Header capitalization consistency + - [ ] React concept capitalization + - [ ] Package name capitalization (react-dom vs React DOM) + - [ ] Format code with prettier + +--- + React 19 Beta is now available on npm! In our [React 19 Upgrade Guide](/blog/04/01/react-19-upgrade-guide), we shared step-by-step instructions for upgrading your app to the React 19 Beta. In this post, we'll give an overview of the new features in React 19 Beta, and how you can adopt them. + + _Note for React Native users: React 19 will ship a future version of React Native with the New React Native Architecture._ @@ -28,7 +45,7 @@ For more see [the React Conf website](https://conf.react.dev). ## What's new in React 19 {/*whats-new-in-react-19*/} -### New Feature: Actions {/*new-feature-actions*/} +### Actions {/*actions*/} 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: @@ -80,20 +97,23 @@ For more information, see the docs for [`useTransition`](/reference/react/useTra 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`: -```js {2,6} +```js {2,9} const [name, setName] = useState(''); -const [submitAction, data, isPending] = useActionState(async () => { - return await updateName(name); +const [submitAction, state, isPending] = useActionState(async () => { + const {error} = await updateName(name); setName(''); - return result; + + // You can return any result of the action. + // Here, we return only the error. + return error; }); ``` -`useActionState` accepts an 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`. +`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`. For more information, see the docs for [`useActionState`](/reference/react/useActionState). -### New Feature: Form Actions {/*new-feature-form-actions*/} +### Form Actions {/*form-actions*/} Actions are also integrated with React 19's new Form features. We've added an `action` prop to React DOM `
` elements to automatically submit forms with Actions: @@ -110,9 +130,9 @@ return ( ) ``` -When a `` Action succeeds, React will automatically reset the form for uncontrolled components. If you need to reset the `` manually, you can call the new [`requestFormReset`](/todo) react-dom API. +When a `` Action succeeds, React will automatically reset the form for uncontrolled components. If you need to reset the `` manually, you can call the new [`requestFormReset`](/todo) React DOM API. -### New Hook: useFormStatus {/*new-hook-useformstatus*/} +### New Hook: `useFormStatus` {/*new-hook-useformstatus*/} In design systems, it's common to write design components that need access to the nearest Form status, without passing the status down to the component. This can be done via Context, but to make the common case easier, we've added a new hook `useFormStatus`: @@ -132,7 +152,7 @@ function NameInput() { For more information, see the docs for [`useFormStatus`](/reference/react-dom/hooks/useFormStatus). -### New Hook: useOptimistic {/*new-feature-optimistic-updates*/} +### New Hook: `useOptimistic` {/*new-feature-optimistic-updates*/} 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: @@ -164,48 +184,568 @@ The `useOptimisitc` hook will immediately render the `optimisticName` while the For more information, see the docs for [`useOptimistic`](/reference/react/useOptimistic). -### New Feature: Server Actions {/*new-feature-server-actions*/} +### New API: `use` {/*new-feature-use*/} -TODO +In React 19 we're introducing a new API to read resources in render: `use`. - -TODO: Requires a bundler and framework that supports Server Actions. - +For example, you can read a promise with `use`, and React will Suspend until the promise resolves: -### New Feature: Server Components {/*new-feature-server-components*/} +```js {1,6} +import {use} from 'react'; -TODO - - -TODO: Requires a bundler and framework that supports RSC. - - -### New Feature: `use` {/*new-feature-use*/} - -TODO - -### New Feature: Resource and Metadata Components {/*new-feature-resource-and-metadata-components*/} - -In HTML, many elements are reserved for placement in the `` section of the document. This includes metadata elements like `` and `<meta>`, and some forms of resource elements like `<script>`, `<style>` and `<link>`. In React, it's often convenient these elements deeper in the tree. - -These elements can be inserted manually in an effect, and libraries like [`react-helmet`](github.com/nfl/react-helmet) have made this easier. In React 19, we're adding support for rendering most `<head>` tags in components natively: - -```js -return ( - <div> - <p>Hello World</p> - <title>Hello World - - - -