Skip to content
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

refactor(react-server): use official createServerReference #283

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,13 @@ export async function actionCheckAnswer(formData: FormData) {
const message = answer === 2 ? "Correct!" : "Wrong!";
return { message };
}

export async function actionStateTest(prevArg: unknown, formData: FormData) {
const result = { prev: prevArg, form: [...formData.entries()] };
console.log("[actionStateTest]", result);
return result;
}

export async function actionBindTest(boundArg: string, formData: FormData) {
console.log("[actionBindTest]", { boundArg, form: [...formData.entries()] });
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { useActionData } from "@hiogawa/react-server/client";
import React from "react";
import ReactDom from "react-dom";
import {
actionBindTest,
actionCheckAnswer,
actionStateTest,
addMessage,
changeCounter,
type getMessages,
Expand Down Expand Up @@ -117,6 +119,39 @@ export function ActionDataTest() {
);
}

// TODO
export function UseActionStateTest() {
const [data, formAction, isPending] = ReactDom.useFormState(
actionStateTest,
null,
);

React.useEffect(() => {
console.log("[useActionState]", data, isPending);
}, [data, isPending]);

return (
<form action={formAction} className="flex flex-col gap-2">
<input type="hidden" name="hello" value="world" />
<button className="antd-input p-1 text-sm max-w-30">
useActionState Test
</button>
</form>
);
}

export function ClientActionBindTest() {
const formAction = actionBindTest.bind(null, "bound!!");
return (
<form action={formAction} className="flex flex-col gap-2">
<input type="hidden" name="hello" value="world" />
<button className="antd-input p-1 text-sm max-w-30">
Client Action Bind Test
</button>
</form>
);
}

// https://react.dev/reference/react-dom/hooks/useFormStatus
export function FormStateTest() {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { changeCounter, getCounter, getMessages } from "./_action";
import {
ActionDataTest,
Chat,
ClientActionBindTest,
Counter,
Counter2,
FormStateTest,
UseActionStateTest,
} from "./_client";

export default async function Page() {
Expand All @@ -17,6 +19,8 @@ export default async function Page() {
</div>
<Chat messages={getMessages()} />
<ActionDataTest />
<UseActionStateTest />
<ClientActionBindTest />
<FormStateTest />
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion packages/react-server/examples/basic/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"moduleResolution": "Bundler",
"module": "ESNext",
"target": "ESNext",
"lib": ["ESNext", "DOM"],
"lib": ["ESNext", "DOM", "DOM.Iterable"],
"types": ["vite/client", "react/experimental", "react-dom/experimental"],
"jsx": "react-jsx"
}
Expand Down
5 changes: 4 additions & 1 deletion packages/react-server/src/entry/browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,10 @@ export async function start() {
if (document.documentElement.dataset["noHydate"]) {
reactDomClient.createRoot(document).render(reactRootEl);
} else {
reactDomClient.hydrateRoot(document, reactRootEl);
reactDomClient.hydrateRoot(document, reactRootEl, {
// @ts-ignore TODO
formState: null,
});
}

// custom event for RSC reload
Expand Down
2 changes: 2 additions & 0 deletions packages/react-server/src/entry/server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ export async function renderHtml(
let status = 200;
try {
ssrStream = await reactDomServer.renderToReadableStream(reactRootEl, {
// @ts-ignore TODO
formState: null,
bootstrapModules: url.search.includes("__noJs")
? []
: assets.bootstrapModules,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { RedirectBoundary } from "../../runtime-client";
import { createError } from "../../server";
import { LayoutStateContext } from "../router/client";

// TODO: replace with React.useActionState
export function useActionData<T extends (...args: any[]) => any>(
action: T,
): Awaited<ReturnType<T>> | undefined {
Expand Down