Skip to content

Commit e1701fa

Browse files
authored
Merge pull request #92 from reactjs/sync-2b2d0f23
2 parents a0c82e1 + 52c61c8 commit e1701fa

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

src/content/learn/react-compiler.md

+23-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ These docs are still a work in progress. More documentation is available in the
2020

2121
<Note>
2222
React Compiler is a new experimental compiler that we've open sourced to get early feedback from the community. It still has rough edges and is not yet fully ready for production.
23-
24-
React Compiler requires React 19 RC. If you are unable to upgrade to React 19, you may try a userspace implementation of the cache function as described in the [Working Group](https://github.com/reactwg/react-compiler/discussions/6). However, please note that this is not recommended and you should upgrade to React 19 when possible.
2523
</Note>
2624

2725
React Compiler is a new experimental compiler that we've open sourced to get early feedback from the community. It is a build-time only tool that automatically optimizes your React app. It works with plain JavaScript, and understands the [Rules of React](/reference/rules), so you don't need to rewrite any code to use it.
@@ -226,6 +224,29 @@ module.exports = function () {
226224

227225
`babel-plugin-react-compiler` should run first before other Babel plugins as the compiler requires the input source information for sound analysis.
228226

227+
React Compiler works best with React 19 RC. If you are unable to upgrade, you can install the extra `react-compiler-runtime` package which will allow the compiled code to run on versions prior to 19. However, note that the minimum supported version is 17.
228+
229+
<TerminalBlock>
230+
npm install react-compiler-runtime@experimental
231+
</TerminalBlock>
232+
233+
You should also add the correct `target` to your compiler config, where `target` is the major version of React you are targeting:
234+
235+
```js {3}
236+
// babel.config.js
237+
const ReactCompilerConfig = {
238+
target: '18' // '17' | '18' | '19'
239+
};
240+
241+
module.exports = function () {
242+
return {
243+
plugins: [
244+
['babel-plugin-react-compiler', ReactCompilerConfig],
245+
],
246+
};
247+
};
248+
```
249+
229250
### Vite {/*usage-with-vite*/}
230251

231252
If you use Vite, you can add the plugin to vite-plugin-react:

src/content/reference/react/useActionState.md

+12-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ In earlier React Canary versions, this API was part of React DOM and called `use
2020
`useActionState` is a Hook that allows you to update state based on the result of a form action.
2121

2222
```js
23-
const [state, formAction] = useActionState(fn, initialState, permalink?);
23+
const [state, formAction, isPending] = useActionState(fn, initialState, permalink?);
2424
```
2525
2626
</Intro>
@@ -35,7 +35,7 @@ const [state, formAction] = useActionState(fn, initialState, permalink?);
3535
3636
{/* TODO T164397693: link to actions documentation once it exists */}
3737
38-
Call `useActionState` at the top level of your component to create component state that is updated [when a form action is invoked](/reference/react-dom/components/form). You pass `useActionState` an existing form action function as well as an initial state, and it returns a new action that you use in your form, along with the latest form state. The latest form state is also passed to the function that you provided.
38+
Call `useActionState` at the top level of your component to create component state that is updated [when a form action is invoked](/reference/react-dom/components/form). You pass `useActionState` an existing form action function as well as an initial state, and it returns a new action that you use in your form, along with the latest form state and whether the Action is still pending. The latest form state is also passed to the function that you provided.
3939
4040
```js
4141
import { useActionState } from "react";
@@ -71,10 +71,11 @@ If used with a Server Action, `useActionState` allows the server's response from
7171
7272
#### Returns {/*returns*/}
7373
74-
`useActionState` returns an array with exactly two values:
74+
`useActionState` returns an array with the following values:
7575
7676
1. The current state. During the first render, it will match the `initialState` you have passed. After the action is invoked, it will match the value returned by the action.
7777
2. A new action that you can pass as the `action` prop to your `form` component or `formAction` prop to any `button` component within the form.
78+
3. The `isPending` flag that tells you whether there is a pending Transition.
7879
7980
#### Caveats {/*caveats*/}
8081
@@ -104,10 +105,11 @@ function MyComponent() {
104105
}
105106
```
106107
107-
`useActionState` returns an array with exactly two items:
108+
`useActionState` returns an array with the following items:
108109
109110
1. The <CodeStep step={1}>current state</CodeStep> of the form, which is initially set to the <CodeStep step={4}>initial state</CodeStep> you provided, and after the form is submitted is set to the return value of the <CodeStep step={3}>action</CodeStep> you provided.
110111
2. A <CodeStep step={2}>new action</CodeStep> that you pass to `<form>` as its `action` prop.
112+
3. A <CodeStep step={1}>pending state</CodeStep> that you can utilise whilst your action is processing.
111113
112114
When the form is submitted, the <CodeStep step={3}>action</CodeStep> function that you provided will be called. Its return value will become the new <CodeStep step={1}>current state</CodeStep> of the form.
113115
@@ -133,13 +135,13 @@ import { useActionState, useState } from "react";
133135
import { addToCart } from "./actions.js";
134136

135137
function AddToCartForm({itemID, itemTitle}) {
136-
const [message, formAction] = useActionState(addToCart, null);
138+
const [message, formAction, isPending] = useActionState(addToCart, null);
137139
return (
138140
<form action={formAction}>
139141
<h2>{itemTitle}</h2>
140142
<input type="hidden" name="itemID" value={itemID} />
141143
<button type="submit">Add to Cart</button>
142-
{message}
144+
{isPending ? "Loading..." : message}
143145
</form>
144146
);
145147
}
@@ -162,6 +164,10 @@ export async function addToCart(prevState, queryData) {
162164
if (itemID === "1") {
163165
return "Added to cart";
164166
} else {
167+
// Add a fake delay to make waiting noticeable.
168+
await new Promise(resolve => {
169+
setTimeout(resolve, 2000);
170+
});
165171
return "Couldn't add to cart: the item is sold out.";
166172
}
167173
}

0 commit comments

Comments
 (0)