Full Stack Components not working as expected in remix@latest #9393
-
Hi! First time remix user. I really like Kent's pattern described here. However I've been unable to get the full stack component to trigger its loader. I see this discussion from a while back talking about how vite may be implicated, but i'm seeing no errors and that import restriction seems to be resolved. Here's a repo that reproduces the issue locally for me. What am I holding wrong? import type { MetaFunction } from "@remix-run/node";
import { useLoaderData } from "@remix-run/react";
import { User } from "./user";
export const meta: MetaFunction = () => {
return [
{ title: "New Remix App" },
{ name: "description", content: "Welcome to Remix!" },
];
};
import { json, LoaderFunction } from "@remix-run/node";
export const loader: LoaderFunction = async () => {
console.log("firing from the root loader");
return json({ message: "This is some JSON data from the index route" });
};
export default function Index() {
const data = useLoaderData<typeof loader>();
return (
<div style={{ fontFamily: "system-ui, sans-serif", lineHeight: "1.8" }}>
<h1>Index Data</h1>
<p>{data.message}</p>
<User />
</div>
);
} import { json, LoaderFunction } from "@remix-run/node";
export const loader: LoaderFunction = async () => {
console.log("firing from the user loader");
return json({ userMessage: "This is some JSON data from the user" });
};
import { useFetcher } from "@remix-run/react";
export function User() {
const fetcher = useFetcher<typeof loader>();
return (
<div>
<h1>User Data</h1>
<p>{fetcher.data ? fetcher.data.userMessage : "Loading..."}</p>
</div>
);
}
Thanks! EDIT: Tried again with a fetcher |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
The problem is that your component in the resource route (User) is using useLoaderData, this will return the loader data of the route rendering the component, Kent's FullStack Component pattern instead uses useFetcher to fetch the resource route loader. Note that this also means that your component will not have initial data (unless you also fetch the data in the UI route loader). |
Beta Was this translation helpful? Give feedback.
He calls the submit method of the fetcher here https://github.com/kentcdodds/full-stack-components/blob/24493d0da4f81206434487bb41a0c940db38dc55/apps/02-after/app/routes/resources/customers.tsx#L40.
I think you're expecting to have data initially, that's not the point of Kent's full stack components, the idea is to use it to build components like a dropdown where you also have associated loader or action that you will need to call after the user interacts with the component.