Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add note to docs on layout useLoaderData restriction #8958

Merged
merged 3 commits into from Mar 4, 2024

Conversation

brophdawg11
Copy link
Contributor

Added some docs to clear up confusion around the inability to use useLoaderData in a Layout component when rendering an error boundary

See #8951 and #8846.

Copy link

changeset-bot bot commented Mar 1, 2024

⚠️ No Changeset found

Latest commit: 1ef72e3

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Comment on lines 156 to 164
export function Layout({
children,
}: {
children: React.ReactNode;
}) {
const data = useRouteLoaderData("root");
const error = useRouteError();
return <html>...</html>;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only suggestion I have is including a little more example of what you would do with this information. Open to a better example, just figure this might help folks understand the purpose a bit better

Suggested change
export function Layout({
children,
}: {
children: React.ReactNode;
}) {
const data = useRouteLoaderData("root");
const error = useRouteError();
return <html>...</html>;
}
export function Layout({
children,
}: {
children: React.ReactNode;
}) {
const data = useRouteLoaderData("root");
const error = useRouteError();
return (
<html>
<head>{/* */}</head>
<body>
{data ? (
<RouteLayout>{children}</RouteLayout>
) : (
<ErrorLayout>{children}</ErrorLayout>
)}
<ScrollRestoration />
<Scripts />
</body>
</html>
);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a dynamic stylesheet link could be a good example and a real use case :) Something like:

<link rel="stylesheet" href={data.themeStylesheetURL} />

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding analytics using an env variable from the root loader is a good use case

If useRouteLoaderData("root") returns a value, you render <Analytics token={loaderData.env.analytics} />, otherwise you don't

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's tricky because there are other ways to do both of these. I prefer I prefer doing the dynamic stylesheet via meta, and the <Analytics> component could probably be done via the root component - but I do understand folks will want to put stuff that feels "global" like this in the Layout. I updated with the Analytics example and the CSS custom property example from the linked discussion. 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brophdawg11 thank you very much! I note that you recommend meta for dynamic stylesheet <link>s but the doc precisely discourages the meta export for stylesheets in favor ot the links one instead (which can't depend on the data):

For asset links like stylesheets and favicons, you should use the links export instead.

I always want to follow the official recommendations but this use case seems to be in a grey area. Could you maybe add to the docs that dynamic links are an exception of what is tolerated in the meta export? It's not ideal though because it's not the intention of this export (made for SEO). Any suggestion? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The link here depended on loader data so it could not be done in links and would have to be done in meta (or could also be done in Layout)

brophdawg11 and others added 2 commits March 4, 2024 10:38
@brophdawg11 brophdawg11 merged commit 0bdbef7 into main Mar 4, 2024
2 checks passed
@brophdawg11 brophdawg11 deleted the brophdawg11/layout-loader-data branch March 4, 2024 16:03
IgnusG pushed a commit to IgnusG/remix that referenced this pull request Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants