Skip to main content

ReactErrorBoundary

Robust error boundary library for Lua React based on react-error-boundary. See ErrorBoundary for the main component usage.

Types

ErrorBoundaryContextType

interface ErrorBoundaryContextType {
didCatchboolean
errorany
resetErrorBoundary(...any) → ()
}

FallbackProps

interface FallbackProps {
errorError
resetErrorBoundary(...any) → ()--

Resets the error boundary and calls onReset if provided. This is useful for reverting state or retrying the render.

}

Props for fallback components. Fallback components should be typed like:

	local Fallback: React.FC<ReactErrorBoundary.FallbackProps> = function(props)
		-- ...
	end

Functions

withErrorBoundary

ReactErrorBoundary.withErrorBoundary(
componentReact.ComponentType<Props>,
errorBoundaryPropstypes.ErrorBoundaryProps
) → ()

This is a higher-order component that makes it easy to add an error boundary to an existing component. See ErrorIn1SecondHOC for a full example.

note

If using luau strict mode, the component must be typed using local Component: React.FC<{ <prop-types> }> = function(props) for types to work.

useErrorBoundary

ReactErrorBoundary.useErrorBoundary() → UseErrorBoundaryApi<E>

Types

interface UseErrorBoundaryApi<E> {
resetBoundary() → ()
showBoundary(errorE) → ()
}

Convenient hook for imperatively invoking or resetting error boundaries.

Invoking the nearest error boundary from an event handler

React only handles errors thrown during render or during component lifecycle methods (e.g., useEffect, componentDidMount). Error thrown in event handlers or in separate lua threads will not be automatically caught.

A convenient pattern is to invoke the nearest error boundary when an error occurs in event handlers or in separate lua threads.

	local Example: React.FC<{}> = function()
		local errorBoundary = useErrorBoundary()

		useEffect(function()
			-- Using promises
			fetchPlayerData(name):andThen(function(res)
				-- Set data in state to rerender
			end):catch(function(error)
				-- Our promise failed, so invoke the nearest error boundary to handle the error
				errorBoundary.showBoundary(error)
			end)

			-- Using pcall
			task.defer(function()
				local success, res = pcall(function()
					-- Do something that might throw an error
				end)

				if not success then
					errorBoundary.showBoundary(res)
				end
			end)
		end)

		-- Render ...
	end

Resetting the nearest error boundary

resetBoundary() requests the nearest boundary to retry the render that originally failed and if passed, invokes the boundary's onReset callback.

A pattern is to just retry the render blindly and hope it succeeds on the second try. See RetryError for a full example.

A more common pattern is to revert some state and retry the render with the reverted state. See ResetCount for a full example.

Show raw api
{
    "functions": [
        {
            "name": "withErrorBoundary",
            "desc": "This is a higher-order component that makes it easy to add an error boundary to an existing component. See\n[ErrorIn1SecondHOC](https://github.com/chriscerie/react-error-boundary/blob/main/stories/ErrorIn1SecondHOC.story.lua) for a full example.\n\n:::note\nIf using luau strict mode, the component must be typed using `local Component: React.FC<{ <prop-types> }> = function(props)`\nfor types to work.\n:::",
            "params": [
                {
                    "name": "component",
                    "desc": "",
                    "lua_type": "React.ComponentType<Props>"
                },
                {
                    "name": "errorBoundaryProps",
                    "desc": "",
                    "lua_type": "types.ErrorBoundaryProps"
                }
            ],
            "returns": [],
            "function_type": "static",
            "source": {
                "line": 18,
                "path": "src/withErrorBoundary.lua"
            }
        },
        {
            "name": "useErrorBoundary",
            "desc": "Convenient hook for imperatively invoking or resetting error boundaries.\n\n#### Invoking the nearest error boundary from an event handler\n\nReact only handles errors thrown during render or during component lifecycle methods (e.g., useEffect, componentDidMount).\nError thrown in event handlers or in separate lua threads will not be automatically caught.\n\nA convenient pattern is to invoke the nearest error boundary when an error occurs in event handlers or in separate lua threads.\n\n```lua\n\tlocal Example: React.FC<{}> = function()\n\t\tlocal errorBoundary = useErrorBoundary()\n\n\t\tuseEffect(function()\n\t\t\t-- Using promises\n\t\t\tfetchPlayerData(name):andThen(function(res)\n\t\t\t\t-- Set data in state to rerender\n\t\t\tend):catch(function(error)\n\t\t\t\t-- Our promise failed, so invoke the nearest error boundary to handle the error\n\t\t\t\terrorBoundary.showBoundary(error)\n\t\t\tend)\n\n\t\t\t-- Using pcall\n\t\t\ttask.defer(function()\n\t\t\t\tlocal success, res = pcall(function()\n\t\t\t\t\t-- Do something that might throw an error\n\t\t\t\tend)\n\n\t\t\t\tif not success then\n\t\t\t\t\terrorBoundary.showBoundary(res)\n\t\t\t\tend\n\t\t\tend)\n\t\tend)\n\n\t\t-- Render ...\n\tend\n```\n\n#### Resetting the nearest error boundary\n\n`resetBoundary()` requests the nearest boundary to retry the render that originally failed and if passed,\ninvokes the boundary's `onReset` callback.\n\nA pattern is to just retry the render blindly and hope it succeeds on the second try. See\n[RetryError](https://github.com/chriscerie/react-error-boundary/blob/main/stories/RetryError.story.lua) for a full example.\n\nA more common pattern is to revert some state and retry the render with the reverted state. See\n[ResetCount](https://github.com/chriscerie/react-error-boundary/blob/main/stories/ResetCount.story.lua) for a full example.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "UseErrorBoundaryApi<E>\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 69,
                "path": "src/useErrorBoundary.lua"
            }
        }
    ],
    "properties": [],
    "types": [
        {
            "name": "UseErrorBoundaryApi<E>",
            "desc": "",
            "fields": [
                {
                    "name": "resetBoundary",
                    "lua_type": "() -> ()",
                    "desc": ""
                },
                {
                    "name": "showBoundary",
                    "lua_type": "(error: E) -> ()",
                    "desc": ""
                }
            ],
            "source": {
                "line": 12,
                "path": "src/useErrorBoundary.lua"
            }
        },
        {
            "name": "ErrorBoundaryContextType",
            "desc": "",
            "fields": [
                {
                    "name": "didCatch",
                    "lua_type": "boolean",
                    "desc": ""
                },
                {
                    "name": "error",
                    "lua_type": "any",
                    "desc": ""
                },
                {
                    "name": "resetErrorBoundary",
                    "lua_type": "(...any) -> ()",
                    "desc": ""
                }
            ],
            "source": {
                "line": 11,
                "path": "src/ErrorBoundaryContext.lua"
            }
        },
        {
            "name": "FallbackProps",
            "desc": "Props for fallback components. Fallback components should be typed like:\n```lua\n\tlocal Fallback: React.FC<ReactErrorBoundary.FallbackProps> = function(props)\n\t\t-- ...\n\tend\n```",
            "fields": [
                {
                    "name": "error",
                    "lua_type": "Error",
                    "desc": ""
                },
                {
                    "name": "resetErrorBoundary",
                    "lua_type": "(...any) -> ()",
                    "desc": "Resets the error boundary and calls `onReset` if provided. This is useful for reverting state or retrying the render."
                }
            ],
            "source": {
                "line": 33,
                "path": "src/types.lua"
            }
        }
    ],
    "name": "ReactErrorBoundary",
    "desc": "Robust error boundary library for Lua React based on react-error-boundary.\nSee [ErrorBoundary](/api/ErrorBoundary) for the main component usage.",
    "source": {
        "line": 19,
        "path": "src/init.lua"
    }
}