Skip to content

Unexpected behaviour with Suspense, Outlet and React.use() #12773

Answered by sergiodxa
luisdlopez asked this question in Q&A
Discussion options

You must be logged in to vote

This is an expected behavior of React, not React Router.

When a Suspense boundary goes from pending to resolved, it won't go back to pending even if the content has a new pending promise. Instead the Suspense boundary will keep the previous UI in place until the new promise is resolved.

To prevent this behavior, you have to force the Suspense boundary to mount again, so it's a new one without a resolved state. A simple way to do it for navigations is to pass the location.key as the key prop.

let location = useLocation()
return (
  <Suspense key={location.key} fallback={<Loading />}>
    <Outlet />
  </Suspense>
)

The location.key change on every navigation so it will help force the re-mou…

Replies: 4 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@luisdlopez
Comment options

Answer selected by luisdlopez
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants