-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(hooks): use separate type for server state in `useOptimistic…
…Action` (#134) This PR improves the design of the `useOptimisticAction` hook. Previously, the `optimisticData` had to be of the same type of action's return type. This isn't great, because the only job of a Server Action in a optimistic workflow is to mutate the data on the server. The actual state (and type) that matters is the one coming from the parent Server Component, so `updateFn`'s `prevState` should match that type. The actual result is still available in hooks callbacks and `result` property returned from the hook, though. re #127
- Loading branch information
Showing
5 changed files
with
88 additions
and
94 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
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
11 changes: 4 additions & 7 deletions
11
apps/playground/src/app/(examples)/optimistic-hook/page.tsx
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 |
---|---|---|
@@ -1,18 +1,15 @@ | ||
import { StyledHeading } from "@/app/_components/styled-heading"; | ||
import { getLikes } from "./addlikes-action"; | ||
import AddLikeForm from "./addlikes-form"; | ||
import { getTodos } from "./addtodo-action"; | ||
import AddTodoForm from "./addtodo-form"; | ||
|
||
export default async function OptimisticHookPage() { | ||
const likesCount = await getLikes(); | ||
const todos = await getTodos(); | ||
|
||
return ( | ||
<main className="w-96 max-w-full px-4"> | ||
<StyledHeading>Action using optimistic hook</StyledHeading> | ||
<pre className="mt-4 text-center"> | ||
Server data: {JSON.stringify(likesCount)} | ||
</pre> | ||
{/* Pass the server state to Client Component */} | ||
<AddLikeForm likesCount={likesCount} /> | ||
<AddTodoForm todos={todos} /> | ||
</main> | ||
); | ||
} |
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