errorBoundary
Description: Defines if the error should be thrown. The suspense property has to be true in order for this to work.
Default Value: false.
- How To: Set
errorBoundary: truewhen you want errors fromuseFetchto be caught by the nearest React Error Boundary. Doing this, you can replace a broken UI section with a standardized Error component rather than rendering half-broken component with missing data. Always use in conjunction withsuspense: true.// Correct: Errors will be caught by the nearest ErrorBoundaryuseFetch('critical-component-data',fetchCriticalData,{ suspense: true, errorBoundary: true }); - Best Practice: Avoid setting
errorBoundary: truewithoutsuspense: true, as it won't function as intended. TheuseFetchhook relies on Suspense to propagate errors to the boundary.// Avoid this: `errorBoundary` won't work without `suspense: true`useFetch('data',() => fetchData(),{suspense: false,errorBoundary: true});
Component Setup
<ErrorBoundary fallback={<ErrorMessage />}>
<Suspense fallback={<LoadingSpinner />}>
<UserList />
</Suspense>
</ErrorBoundary>
Why
- Errors are propagated to the nearest Error Boundary
- Prevents component errors from crashing the entire app
- Keeps UI predictable and fault-tolerant