You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/routes/concepts/control-flow/error-boundary.mdx
+31-18Lines changed: 31 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,26 +3,39 @@ title: "Error boundary"
3
3
order: 5
4
4
---
5
5
6
-
`<ErrorBoundary>` is a component that can be used to catch errors thrown by child components.
7
-
When encountering an error, this component will render a fallback UI instead of the problematic child component(s).
6
+
By default, if part of an application throws an error during rendering, the entire application can crash, resulting in Solid removing its UI from the screen.
7
+
Error boundaries provide a way to catch these errors and prevent the entire app from crashing.
The [`<ErrorBoundary>`](/reference/components/error-boundary) component is used to create an error boundary.
10
+
It catches any error that occurs during the rendering or updating of its children.
11
+
However, an important note is that errors occurring outside the rendering process, such as in event handlers or after a `setTimeout`, are _not_ caught by error boundaries.
16
12
17
-
`<ErrorBoundary>` accepts a `fallback` prop that can be used to render a custom error message, or to provide a friendly notification to the user.
18
-
This prop accepts a function that receives the caught error as an argument, providing a flexible way to handle different error scenarios.
13
+
The `fallback` prop can be used to display a user-friendly error message or notification when an error occurs.
14
+
If a function is passed to `fallback`, it will receive the error object as well as a `reset` function.
15
+
The `reset` function forces the `<ErrorBoundary>` to re-render its children and reset the error state, providing users with a way to recover from the error.
By wrapping parts of your application in `<ErrorBoundary>`, you can prevent the entire application from crashing when an error occurs due to a single component.
21
+
function App() {
22
+
return (
23
+
<div>
24
+
<Header />
25
+
<ErrorBoundary
26
+
fallback={(error, reset) => (
27
+
<div>
28
+
<p>Something went wrong: {error.message}</p>
29
+
<buttononClick={reset}>Try Again</button>
30
+
</div>
31
+
)}
32
+
>
33
+
<ErrorProne />
34
+
</ErrorBoundary>
35
+
</div>
36
+
);
37
+
}
38
+
```
26
39
27
-
When an error is encountered, the `<ErrorBoundary>`component will catch the error and render the fallback UI instead of the problematic component(s).
28
-
This way, even when a component fails, the user has a controlled UI response instead of a broken interface.
40
+
In this example, when the `ErrorProne` component throws an error, the `<ErrorBoundary>`catches it, preventing it from affecting the rest of the application.
41
+
Instead, it displays the error message passed to the fallback prop.
However, errors occurring outside the rendering process are **not** captured by error boundaries.
14
+
For instance:
17
15
18
-
Here's an example of how to use it:
16
+
- Errors that occur inside event handlers.
17
+
- Errors that occur after a `setTimeout`.
19
18
20
-
```tsx
21
-
<ErrorBoundary fallback={<div>Something went terribly wrong</div>}>
22
-
<MyComp />
23
-
</ErrorBoundary>
24
-
```
19
+
## Props
25
20
26
-
Ifyouwanttocustomizetheerrormessage, youcanpassafunction as the `fallback` prop. The function will be called with the error and a `reset` function. The `reset` function will reset the error boundary and re-render the children.
0 commit comments