How to show loading state while refetching? #50769
Replies: 7 comments 2 replies
-
Nice catch mate , same question here! |
Beta Was this translation helpful? Give feedback.
-
I think I understand how to achieve this. Though, I don't yet understand how it works. It also feels a bit limiting and disconnected. Maybe it's just me. Wrap your router methods in the start transition function from the useTransition hook, then you can control the loading state by isPending value from the same hook. |
Beta Was this translation helpful? Give feedback.
-
In production you cant really avoid to use client libraries like swr and tanstack query. Maybe use those for your search. But you can do what you want by wrapping with Suspense and you need to move some of your code. <Suspense fallback={<p>Loading feed...</p>}>
<PostFeed /> // any component that does async
</Suspense> |
Beta Was this translation helpful? Give feedback.
-
In this particular case you need to use Streaming with Suspense.
If you use Streaming with Suspense it will load after your main However it's not enough to get visual feedback when we fetch new data. To make fallback component appear on every request, we have to add key prop to Suspense component.
Here is codesandbox link: https://codesandbox.io/p/sandbox/next-playground-forked-zlp7kx |
Beta Was this translation helpful? Give feedback.
-
I know this is a while ago, but adding a key like @eneskabacaoglu showed solved it for me! I hadn't read up on React Suspense, hence why I didn't know about it. Explanation here. |
Beta Was this translation helpful? Give feedback.
-
what about if i need a property to tell me , so i can disable a button while it revalidate ? |
Beta Was this translation helpful? Give feedback.
-
Is there a way to show a loading state based on the page refreshing? A use case I have is a basic table that is given data by server component ( page ) and use a form of inputs ( filters ) to update the url and refresh data based on search queries. When the user submits the form, nextjs ( refreshes the page right? With the input's name and value as search queries right? ). So when refreshing the page, the call to fetch the data runs again, but this time the loading screen doesn't appear. Any solutions or insights? |
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.
-
Example: https://codesandbox.io/p/sandbox/next-playground-xdr82j
Hi!
As you can see in the example, the loading state is only shown on the first render, but not after the url changes or a refresh is requested.
I like the "optimistic way" of fetching, showing the current state while new data is being fetch, but I would like to show an indicator to the user during the fetching so they know "something is happening". Is it possible?
Also, is it possible for the
<Link />
component and theuseRouter()
hook to force a refresh (ignore client cache) on a redirect likerouter.refresh()
does with the current route? It would be nice to be able to invalidate routes in the client's cache as well.Beta Was this translation helpful? Give feedback.
All reactions