Remix SPA - clientLoader and Suspense not working as I would expect #9159
-
I posted on Discord originally, but didn't get a response - so apologies for duplicating here. I'm really keen to move from CRA to Remix and the below is blocking me. I'm stumbling into some issues around loading states and how I expect them to work. For whatever reason, I can't seem to get defer / Suspense to work as it does in server loaders, when using clientLoader. I've got a basic repo here (ignore the tailwind that doesn't show, there was some copy and pasting going on!): https://stackblitz.com/~/github.com/andrewbarnesweb/remix-spa-sandbox I've come from a traditional Create React App (CRA) background, so my experience if I'm not lazy loading, is the page would load straight away and any Suspense would kick in in the new page. I'm seeing some different behaviours depending on what I use.
Not sure if I'm approaching this wrong. Or my mindset is wrong coming from CRA background. I understand I could have some useNavigation running in the parent routes to clean up some of this, but I want to load in the static assets and frames as soon as possible and use suspense to fill in the bits I'm waiting for. Any advice would be greatly appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
With I created a PR in your repo. export const clientLoader = async () => {
const myPromise = new Promise<string>((resolve) => {
setTimeout(() => {
resolve(Math.random().toString());
}, 2000);
});
// return the promise
return defer({ myPromise });
};
function SearchResults() {
const { myPromise } = useLoaderData<typeof clientLoader>();
return (
<div>
Show me straight away
<div>
<Suspense fallback={<div>Loading locally</div>}>
{/* here is where Remix awaits the promise */}
<Await resolve={myPromise}>
{/* now you have the resolved value */}
{(randomValue) => (
<div>
<h1>{randomValue}</h1>
</div>
)}
</Await>
</Suspense>
</div>
</div>
);
} |
Beta Was this translation helpful? Give feedback.
With
defer
, Remix expects you to return the Promise, not the awaited value. If youawait
the promise, Remix will wait until it is resolved before rendering.I created a PR in your repo.