Can I add "loading" state to a component until data is ready in loader() function #3304
Replies: 4 comments 2 replies
-
You can use the useTransition hook to know when a transition is happening, but that's only going to work for client-side navigation, for the initial document request (when the user get HTML) there's now way because the user didn't loaded anything yet. If you want to do it on the initial document request you can switch to useFetcher to fetch the data client-side, on SSR you will send a loading state and then once JS loaded fetch the data, but I don't really recommend you to do that for everything, specially on a list of products you should render HTML with content so it can be indexed. |
Beta Was this translation helpful? Give feedback.
-
Thanks for your opinion. I coming from NextJS which has the same limitation. I definitely dont want to switch to useFetcher because then I am back to client side rendering, which has its own limitations. I am not sure if the synchronous nature of loading is a feature, a limitation, a bug or a design decision. The LoaderFunction is async so it can return a promise, but it appears that the useLoaderData does not process as a promise and it prevents rendering of the entire component while waiting for data. This could work if useLoaderData returned a promise, not the data, then it would be possible to continue rendering the rest of the component and display a loading skeleton whilst waiting for data. Is this something that is roadmapped for Remix in the future. It certainly would be a great feature and give Remix another advantage over Next, which I believe has no roadmap for truly asynchronous data loading. |
Beta Was this translation helpful? Give feedback.
-
For furthering (my own) understanding, when you said: "for the initial document request (when the user get HTML) there's now way because the user didn't loaded anything yet." given that the LoaderFunction is async and can return a promise, useLoaderData has the potential to process the promise if Remix could transfer the promise resolution across the network. |
Beta Was this translation helpful? Give feedback.
-
Thanks for your advice. For further clarity and understanding:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm using the following code to create a /products page. I have introduced a delay into the loader and I was hoping to render the component with a "loading" message until the data was ready. Running this code delays render of the whole component under the data is returned form the loader. Can i load the products page with a "loading" state until the data is ready, and then display the data?
Beta Was this translation helpful? Give feedback.
All reactions