Skip to content

Commit 8aa62c4

Browse files
committed
Add error boundary to Flight fixture
Errors in form actions are now rethrown during render (facebook#26689), so we can handle the using an error boundary.
1 parent fd3fb8e commit 8aa62c4

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
}

fixtures/flight/src/Form.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
'use client';
22

33
import * as React from 'react';
4+
import ErrorBoundary from './ErrorBoundary.js';
45

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

89
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>
10+
<ErrorBoundary>
11+
<form
12+
action={async formData => {
13+
setIsPending(true);
14+
try {
15+
const result = await action(formData);
16+
alert(result);
17+
} finally {
18+
setIsPending(false);
19+
}
20+
}}>
21+
<input name="name" />
22+
<button>Say Hi</button>
23+
</form>
24+
</ErrorBoundary>
2425
);
2526
}

0 commit comments

Comments
 (0)