Error handling in root.tsx as soon as loader is in use #9681
-
I started a new project by running In the import {
Links,
Meta,
Outlet,
Scripts,
ScrollRestoration,
useLoaderData,
} from "@remix-run/react";
import "./tailwind.css";
import { LoaderFunctionArgs } from "@remix-run/node";
export const loader = async ({ request }: LoaderFunctionArgs) => {
return {
message: "loader",
};
};
export function Layout({ children }: { children: React.ReactNode }) {
const { message } = useLoaderData<typeof loader>();
return (
<html lang="en">
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<Meta />
<Links />
</head>
<body>
{children}
<ScrollRestoration />
<Scripts />
</body>
</html>
);
}
export default function App() {
return <Outlet />;
} In general this is working fine as long no errors occur somewhere else. As soon I open an unknown route like localhost:3000/test I get the following error:
Without the loader I get a 404 screen, and on the index page I get the value is available like expected. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
The |
Beta Was this translation helpful? Give feedback.
The
Layout
element is supposed to be used to render html markup in both the case of an error and a valid page, which means you can't use auseLoaderData
hook inside of it because in case of anErrorBoundary
it doesn't exist. Try moving your useLoaderData to theApp
export. Whatever you return from theApp
will be rendered inside theLayout
aschildren