-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
eaeec9a
commit 0bfff69
Showing
1 changed file
with
46 additions
and
0 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,46 @@ | ||
--- | ||
"@astrojs/react": minor | ||
"astro": minor | ||
--- | ||
|
||
Add support for [the React 19 `useActionState()` hook](https://react.dev/reference/react/useActionState) when using Astro Actions. This introduces progressive enhancement when calling an Action with the `withState()` utility. | ||
|
||
This example calls a `like` action that accepts a `postId` and returns the number of likes. Pass this action to the `experimental_withState()` function to apply progressive enhancement info, and apply to `useActionState()` to track the result: | ||
|
||
```tsx | ||
import { actions } from 'astro:actions'; | ||
import { experimental_withState } from '@astrojs/react/actions'; | ||
|
||
export function Like({ postId }: { postId: string }) { | ||
const [state, action, pending] = useActionState( | ||
experimental_withState(actions.like), | ||
0, // initial likes | ||
); | ||
|
||
return ( | ||
<form action={action}> | ||
<input type="hidden" name="postId" value={postId} /> | ||
<button disabled={pending}>{state} ❤️</button> | ||
</form> | ||
); | ||
} | ||
``` | ||
|
||
You can also access the state stored by `useActionState()` from your action `handler`. Call `experimental_getActionState()` with the API context, and optionally apply a type to the result: | ||
|
||
```ts | ||
import { defineAction, z } from 'astro:actions'; | ||
import { experimental_getActionState } from '@astrojs/react/actions'; | ||
|
||
export const server = { | ||
like: defineAction({ | ||
input: z.object({ | ||
postId: z.string(), | ||
}), | ||
handler: async ({ postId }, ctx) => { | ||
const currentLikes = experimental_getActionState<number>(ctx); | ||
// write to database | ||
return currentLikes + 1; | ||
} | ||
}) | ||
} |