-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
64b97bc
commit 308fbaa
Showing
8 changed files
with
599 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
export const title = "Async Actions" | ||
export const doc = 'https://react.dev/reference/react/useActionState' | ||
export const playground = 'https://stackblitz.com/edit/vitejs-vite-yttb7n?file=src%2FApp.jsx' | ||
|
||
export const steps = | ||
[ | ||
["Async Actions", | ||
`function StatefulForm() { | ||
const [count, incrementCount] = React.useActionState( | ||
(previousCount) => previousCount + 1, | ||
0 | ||
); | ||
return ( | ||
<form> | ||
{count} | ||
<button formAction={incrementCount}>Increment</button> | ||
</form> | ||
); | ||
}`], | ||
["Make <code>useActionState</code> callback asynchronous", | ||
`function StatefulForm() { | ||
const [count, incrementCount] = React.useActionState( | ||
async (previousCount) => previousCount + 1, | ||
0 | ||
); | ||
return ( | ||
<form> | ||
{count} | ||
<button formAction={incrementCount}>Increment</button> | ||
</form> | ||
); | ||
}`], ["Insert some async behavior into Action callback", | ||
`function StatefulForm() { | ||
const [count, incrementCount] = React.useActionState( | ||
async (previousCount) => { | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
return previousCount + 1; | ||
}, | ||
0 | ||
); | ||
return ( | ||
<form> | ||
{count} | ||
<button formAction={incrementCount}>Increment</button> | ||
</form> | ||
); | ||
}`], ["Destructuer <code>isPending</code> from third item in <code>useActionState</code> tuple", | ||
`function StatefulForm() { | ||
const [count, incrementCount, isPending] = React.useActionState( | ||
async (previousCount) => { | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
return previousCount + 1; | ||
}, | ||
0 | ||
); | ||
return ( | ||
<form> | ||
{count} | ||
<button formAction={incrementCount}>Increment</button> | ||
</form> | ||
); | ||
}`], ["Use <code>isPending</code> to show loading state for the form", | ||
`function StatefulForm() { | ||
const [count, incrementCount, isPending] = React.useActionState( | ||
async (previousCount) => { | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
return previousCount + 1; | ||
}, | ||
0 | ||
); | ||
return ( | ||
<form> | ||
{count} | ||
<button formAction={incrementCount}>Increment</button> | ||
{isPending && '🌀'} | ||
</form> | ||
); | ||
}`], | ||
["✅", | ||
`function StatefulForm() { | ||
const [count, incrementCount, isPending] = React.useActionState( | ||
async (previousCount) => { | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
return previousCount + 1; | ||
}, | ||
0 | ||
); | ||
return( | ||
<form> | ||
{count} | ||
<button formAction={incrementCount}>Increment</button> | ||
{isPending && '🌀'} | ||
</form> | ||
); | ||
}`] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.