Skip to content

Commit

Permalink
Add useFormStatus to Flight fixture (#26773)
Browse files Browse the repository at this point in the history
  • Loading branch information
acdlite authored May 3, 2023
1 parent 2c1117a commit f533cee
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
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

0 comments on commit f533cee

Please sign in to comment.