File tree Expand file tree Collapse file tree 3 files changed +54
-32
lines changed
Expand file tree Collapse file tree 3 files changed +54
-32
lines changed Original file line number Diff line number Diff line change 11'use client' ;
22
33import * as React from 'react' ;
4+ import { flushSync } from 'react-dom' ;
5+ import ErrorBoundary from './ErrorBoundary.js' ;
46
57export default function Button ( { action, children} ) {
68 const [ isPending , setIsPending ] = React . useState ( false ) ;
79
810 return (
9- < form >
10- < button
11- disabled = { isPending }
12- formAction = { async ( ) => {
13- setIsPending ( true ) ;
14- try {
15- const result = await action ( ) ;
16- console . log ( result ) ;
17- } catch ( error ) {
18- console . error ( error ) ;
19- } finally {
20- setIsPending ( false ) ;
21- }
22- } } >
23- { children }
24- </ button >
25- </ form >
11+ < ErrorBoundary >
12+ < form >
13+ < button
14+ disabled = { isPending }
15+ formAction = { async ( ) => {
16+ // TODO: Migrate to useFormPending once that exists
17+ flushSync ( ( ) => setIsPending ( true ) ) ;
18+ try {
19+ const result = await action ( ) ;
20+ console . log ( result ) ;
21+ } finally {
22+ React . startTransition ( ( ) => setIsPending ( false ) ) ;
23+ }
24+ } } >
25+ { children }
26+ </ button >
27+ </ form >
28+ </ ErrorBoundary >
2629 ) ;
2730}
Original file line number Diff line number Diff line change 1+ 'use client' ;
2+
3+ import * as React from 'react' ;
4+
5+ export default class ErrorBoundary extends React . Component {
6+ state = { error : null } ;
7+ static getDerivedStateFromError ( error ) {
8+ return { error} ;
9+ }
10+ render ( ) {
11+ if ( this . state . error ) {
12+ return < div > Caught an error: { this . state . error . message } </ div > ;
13+ }
14+ return this . props . children ;
15+ }
16+ }
Original file line number Diff line number Diff line change 11'use client' ;
22
33import * as React from 'react' ;
4+ import { flushSync } from 'react-dom' ;
5+ import ErrorBoundary from './ErrorBoundary.js' ;
46
57export default function Form ( { action, children} ) {
68 const [ isPending , setIsPending ] = React . useState ( false ) ;
79
810 return (
9- < form
10- action = { async formData => {
11- setIsPending ( true ) ;
12- try {
13- const result = await action ( formData ) ;
14- alert ( result ) ;
15- } catch ( error ) {
16- console . error ( error ) ;
17- } finally {
18- setIsPending ( false ) ;
19- }
20- } } >
21- < input name = "name" />
22- < button > Say Hi</ button >
23- </ form >
11+ < ErrorBoundary >
12+ < form
13+ action = { async formData => {
14+ // TODO: Migrate to useFormPending once that exists
15+ flushSync ( ( ) => setIsPending ( true ) ) ;
16+ try {
17+ const result = await action ( formData ) ;
18+ alert ( result ) ;
19+ } finally {
20+ React . startTransition ( ( ) => setIsPending ( false ) ) ;
21+ }
22+ } } >
23+ < input name = "name" />
24+ < button > Say Hi</ button >
25+ </ form >
26+ </ ErrorBoundary >
2427 ) ;
2528}
You can’t perform that action at this time.
0 commit comments