ErrorBoundary
React.React_Component<PropsWithRef<PropsWithChildren<ErrorBoundaryProps>>, ErrorBoundaryState>
This is a React component. Wrap an ErrorBoundary
around other React components to "catch" errors
and render a fallback UI. The component supports several ways to render a fallback (shown below).
First, create a fallback component.
local e = React.createElement
local Fallback: React.FC<ReactErrorBoundary.FallbackProps> = function(_props)
return e("TextLabel", {
AnchorPoint = Vector2.new(0.5, 0.5),
Size = UDim2.fromScale(0.5, 0.5),
Position = UDim2.fromScale(0.5, 0.5),
Text = "An error was encountered!.",
})
end
Next, in your other components, wrap in an ErrorBoundary
where needed. When an error is encountered somewhere in your app,
the nearest ErrorBoundary
will catch and handle it. You don't need to wrap every component in an ErrorBoundary
. Consider
the granularity of error boundaries and where it makes sense to display an error
message or revert states.
ErrorBoundary
requires a fallback component to be passed in as props. There are 3 ways to pass fallback components. See their
types documentation for usage:
onError
When passed, onError
gets called when an error is caught. This is useful for logging errors.
return e(ReactErrorBoundary.ErrorBoundary, {
FallbackComponent = Fallback,
onError = function(error: LuauPolyfill.Error, info: { componentStack: string })
warn("Caught error:", error, info)
end,
}, {
ComponentThatErrors = e(ComponentThatErrors),
})
onReset
When passed, onReset
gets called when the error boundary is reset by a call to resetErrorBoundary
or when the resetKeys
change.
This is useful for reverting state.
See ResetCount for a full example.
return e(ReactErrorBoundary.ErrorBoundary, {
FallbackComponent = Fallback,
onReset = function(
details: { reason: "imperative-api",
args: { any } } | { reason: "keys", prev: { any }?, next: { any }? }
)
warn("Error boundary was reset:", details)
end,
}, {
ComponentThatErrors = e(ComponentThatErrors),
})
Types
ErrorBoundarySharedProps
interface
ErrorBoundarySharedProps {
onReset:
(
details:
{
reason:
"imperative
-
api"
,
args:
{
any
}
}
|
{
reason:
"keys"
,
prev:
{
any
}
?
,
next:
{
any
}
?
}
)
→
(
)
?
resetKeys:
{
any
}
?
}
ErrorBoundaryPropsWithComponent
interface
ErrorBoundaryPropsWithComponent {
}
One of 3 types of fallback that can be provided to an error boundary.
See ErrorIn1SecondFallbackComponent for a full example.
return e(ReactErrorBoundary.ErrorBoundary, {
FallbackComponent = Fallback,
}, {
ComponentThatErrors = e(ComponentThatErrors),
})
ErrorBoundaryPropsWithRender
interface
ErrorBoundaryPropsWithRender {
fallback:
never?
FallbackComponent:
never?
fallbackRender:
FallbackRender
}
One of 3 types of fallback that can be provided to an error boundary.
Render prop function that returns the fallback UI. This is helpful if you want to handle errors differently based on the error.
See ErrorIn1SecondFallbackComponent for a full example.
caution
Render prop functions are normal functions and are not React components. Attempting to use hooks in them will error!
return e(ReactErrorBoundary.ErrorBoundary, {
fallbackRender = function(props: ReactErrorBoundary.FallbackProps)
return e(Fallback)
end,
}, {
ComponentThatErrors = e(ComponentThatErrors),
})
ErrorBoundaryPropsWithFallback
interface
ErrorBoundaryPropsWithFallback {
fallback:
React.ReactNode
FallbackComponent:
never?
fallbackRender:
never?
}
One of 3 types of fallback that can be provided to an error boundary.
See ErrorIn1SecondFallback for a full example.
caution
This creates the fallback element even when it's not needed.
return e(ReactErrorBoundary.ErrorBoundary, {
fallback = e(Fallback),
}, {
ComponentThatErrors = e(ComponentThatErrors),
})
ErrorBoundaryProps
type
ErrorBoundaryProps =
ErrorBoundarySharedProps
&
(
ErrorBoundaryPropsWithComponent
|
ErrorBoundaryPropsWithRender
|
ErrorBoundaryPropsWithFallback
)