Components / Feedback and display
ErrorBoundary
Catches errors thrown while its children render and shows a fallback in their place, so one crashing part of the app leaves the rest working. The user can try again, and moving on (a new resetKey) recovers by itself.
When to useLink to "When to use"
- Around each part that can fail on its own: a tab's body, a side panel, a chart, a plugin's view.
- Once around the whole app, as the last line, with a fallback that offers a reload.
Use something else when:
- an operation failed (a request, a save): show it where it happened with a Notice or a Toast. Boundaries only catch errors thrown while rendering, not in event handlers or asynchronous code.
- there is nothing to show yet: EmptyState.
UsageLink to "Usage"
import { ErrorBoundary } from "@arshad-shah/cynosure";
<ErrorBoundary label="Query results" resetKey={tabId} onError={(error, info) => report(error, info.componentStack)}>
<ResultsPanel />
</ErrorBoundary>Healthy children render as they are, with no wrapper element, so a boundary never changes the layout of a flex or grid parent.
Retry and resetLink to "Retry and reset"
The default fallback's "Try again" button renders the children again; if they still throw, the fallback comes back. A change of resetKey (compared with Object.is) does the same without the button: pass what the content depends on, such as the active tab or route.
Your own fallbackLink to "Your own fallback"
fallback gets the error and retry. Anything thrown that is not an Error (a string, say) arrives wrapped in one.
<ErrorBoundary
fallback={(error, retry) => (
<EmptyState title="The chart could not be drawn" description={error.message} actions={<Button onClick={retry}>Reload</Button>} />
)}
>
<Chart />
</ErrorBoundary>NestingLink to "Nesting"
The nearest boundary catches the error: an inner one around the editor keeps the window's sidebar and status bar on screen, and the outer one's onError is not called.
PropsLink to "Props"
| Prop | What it does | Type | Default |
|---|---|---|---|
children | The part of the app this boundary protects. Rendered as it is, with no wrapper element. | ReactNode | |
fallback | What to show instead of children after they threw while rendering: gets the error (anything thrown that is not an Error is wrapped in one) and retry, which renders the children again. Default: a danger Notice naming the part (label), with the error's message and a "Try again" button. | ((error: Error, retry: () => void) => ReactNode) | |
resetKey | When this value changes (compared with Object.is), a caught error is cleared and the children render again: pass what the content depends on (a tab or route id) so moving on recovers. | unknown | |
onError | Called once per caught error, with React's component stack, for logging or reporting. | ((error: Error, info: ErrorInfo) => void) | |
label | Names the protected part in the default fallback's heading ("Query results could not be shown"). | string | |
labels | Replace the built-in English text of the default fallback: its heading and the retry button. | Partial<ErrorBoundaryLabels> | |
className | Added to the default fallback's root, for layout. | string | |
style | Layout only, on the default fallback's root. Restyle through --cy-error-boundary-* tokens. | CSSProperties |
Generated from the type definitions in @arshad-shah/cynosure 0.5.0.
Behaviour and accessibilityLink to "Behaviour and accessibility"
| Input | Result |
|---|---|
| Tab | Reaches "Try again" in the normal order; focus is not moved when the fallback appears |
| Enter / Space on "Try again" | Renders the children again |
- Announcement: the default fallback is a danger Notice (
role="alert"), so the failure is announced when it appears. Its heading names the part withlabel("Query results could not be shown"; "Something went wrong" without one), and the error's message follows. - Reporting:
onError(error, info)is called once per caught error, with React's component stack ininfo.componentStack. - Labels:
labelsreplaces the English text: atitle(label)function and theretrybutton's text.
TouchLink to "Touch"
The retry button is a small Button, with a 44px hit area on coarse pointers.
ThemingLink to "Theming"
| Token | What it sets |
|---|---|
--cy-error-boundary-padding | Space around the default fallback, inside the area it replaces |
--cy-error-boundary-max-width | Widest the default fallback's message grows |
The message itself is a Notice: restyle it with --cy-notice-* tokens.
RelatedLink to "Related"
These examples run Cynosure 0.5.0 (commit 50ce30b), the version on npm.