-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Actions: support React 19 useActionState()
with progressive enhancement
#11074
Merged
bholmesdev
merged 23 commits into
feat/react-19-basic-actions
from
feat/react-19-action-state
May 21, 2024
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
0f45b56
feat(ex): Like with useActionState
bholmesdev 4493b37
feat: useActionState progressive enhancement!
bholmesdev 3787410
feat: getActionState utility
bholmesdev 7dc8276
chore: revert actions-blog fixture experimentation
bholmesdev 502b164
fix: add back actions.ts export
bholmesdev 19939db
feat(test): Like with use action state test
bholmesdev c4c5918
fix: stub form state client-side to avoid hydration error
bholmesdev 1c0a3fa
fix: bad .safe chaining
bholmesdev b3488ed
fix: update actionState for client call
bholmesdev cbb3f47
fix: correctly resume form state client side
bholmesdev e56655c
refactor: unify and document reactServerActionResult
bholmesdev 24c76d1
feat(test): useActionState assertions
bholmesdev 9ac35f4
feat(docs): explain my mess
bholmesdev 9063732
refactor: add experimental_ prefix
bholmesdev 1ed6e54
refactor: move all react internals to integration
bholmesdev abe868d
chore: remove unused getIslandProps
bholmesdev 8ceea58
chore: remove unused imports
bholmesdev 35eb988
chore: undo format changes
bholmesdev 18c7669
refactor: get actionResult from middleware directly
bholmesdev f8fab7d
refactor: remove bad result type
bholmesdev eaeec9a
fix: like button disabled timeout
bholmesdev 0bfff69
chore: changeset
bholmesdev 93480e7
refactor: remove request cloning
bholmesdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; | ||
} | ||
}) | ||
} |
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
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
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
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
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
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part is very small and probably can't be moved out. So I don't see any issue with having this part.