diff --git a/fixtures/flight/src/Button.js b/fixtures/flight/src/Button.js index 78adf641139a..d4a3f8500eaf 100644 --- a/fixtures/flight/src/Button.js +++ b/fixtures/flight/src/Button.js @@ -1,27 +1,30 @@ 'use client'; import * as React from 'react'; +import {flushSync} from 'react-dom'; +import ErrorBoundary from './ErrorBoundary.js'; export default function Button({action, children}) { const [isPending, setIsPending] = React.useState(false); return ( -
- -
+ +
+ +
+
); } diff --git a/fixtures/flight/src/ErrorBoundary.js b/fixtures/flight/src/ErrorBoundary.js new file mode 100644 index 000000000000..44dc0965142e --- /dev/null +++ b/fixtures/flight/src/ErrorBoundary.js @@ -0,0 +1,16 @@ +'use client'; + +import * as React from 'react'; + +export default class ErrorBoundary extends React.Component { + state = {error: null}; + static getDerivedStateFromError(error) { + return {error}; + } + render() { + if (this.state.error) { + return
Caught an error: {this.state.error.message}
; + } + return this.props.children; + } +} diff --git a/fixtures/flight/src/Form.js b/fixtures/flight/src/Form.js index 8b1bb01ad0e0..8c2ff2922da1 100644 --- a/fixtures/flight/src/Form.js +++ b/fixtures/flight/src/Form.js @@ -1,25 +1,28 @@ 'use client'; import * as React from 'react'; +import {flushSync} from 'react-dom'; +import ErrorBoundary from './ErrorBoundary.js'; export default function Form({action, children}) { const [isPending, setIsPending] = React.useState(false); return ( -
{ - setIsPending(true); - try { - const result = await action(formData); - alert(result); - } catch (error) { - console.error(error); - } finally { - setIsPending(false); - } - }}> - - -
+ +
{ + // TODO: Migrate to useFormPending once that exists + flushSync(() => setIsPending(true)); + try { + const result = await action(formData); + alert(result); + } finally { + React.startTransition(() => setIsPending(false)); + } + }}> + + +
+
); }