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

Add useFormStatus to Flight fixture #26773

Merged
merged 1 commit into from
May 3, 2023
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
33 changes: 17 additions & 16 deletions fixtures/flight/src/Button.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
'use client';

import * as React from 'react';
import {flushSync} from 'react-dom';
import {experimental_useFormStatus as useFormStatus} from 'react-dom';
import ErrorBoundary from './ErrorBoundary.js';

export default function Button({action, children}) {
const [isPending, setIsPending] = React.useState(false);
function ButtonDisabledWhilePending({action, children}) {
const {pending} = useFormStatus();
return (
<button
disabled={pending}
formAction={async () => {
const result = await action();
console.log(result);
}}>
{children}
</button>
);
}

export default function Button({action, children}) {
return (
<ErrorBoundary>
<form>
<button
disabled={isPending}
formAction={async () => {
// TODO: Migrate to useFormPending once that exists
flushSync(() => setIsPending(true));
try {
const result = await action();
console.log(result);
} finally {
React.startTransition(() => setIsPending(false));
}
}}>
<ButtonDisabledWhilePending action={action}>
{children}
</button>
</ButtonDisabledWhilePending>
</form>
</ErrorBoundary>
);
Expand Down
19 changes: 9 additions & 10 deletions fixtures/flight/src/Form.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
'use client';

import * as React from 'react';
import {flushSync} from 'react-dom';
import {experimental_useFormStatus as useFormStatus} from 'react-dom';
import ErrorBoundary from './ErrorBoundary.js';

function Status() {
const {pending} = useFormStatus();
return pending ? 'Saving...' : null;
}

export default function Form({action, children}) {
const [isPending, setIsPending] = React.useState(false);

return (
<ErrorBoundary>
<form
action={async formData => {
// TODO: Migrate to useFormPending once that exists
flushSync(() => setIsPending(true));
try {
const result = await action(formData);
alert(result);
} finally {
React.startTransition(() => setIsPending(false));
}
const result = await action(formData);
alert(result);
}}>
<label>
Name: <input name="name" />
Expand All @@ -27,7 +26,7 @@ export default function Form({action, children}) {
File: <input type="file" name="file" />
</label>
<button>Say Hi</button>
{isPending ? 'Saving...' : null}
<Status />
</form>
</ErrorBoundary>
);
Expand Down