Replies: 17 comments 8 replies
-
Currently trying to implement Lazy Loading myself... and running into this flickering issue. I've tried using useTransition myself, but having a hard time wrapping my head around it. Could you show me a code-snippet of what you mean by "manually mark navigation events as transitions"? It would help in the interim :) |
Beta Was this translation helpful? Give feedback.
-
The documentation for upgrading to v6 (https://reactrouter.com/docs/en/v6/upgrading/v5) specifies that react-router is now using I though by reading the documentation that react-router v6 already had full support for suspense and transitions, but it seems it does not? |
Beta Was this translation helpful? Give feedback.
-
I believe that hook is going to be renamed to |
Beta Was this translation helpful? Give feedback.
-
Is there any code-snippets show how to implement lazy load without flickering? I'm a noob and facing the same problem using lazy load with react-router. |
Beta Was this translation helpful? Give feedback.
-
I'm not sure if you are doing something similar to me, but for me the flicker is caused because the lazy load's fallback is a blank page. Because of this, there is a small time in which this page is loaded. I need to work on something internally for this, but sorry I wish I had code for avoiding flicker. It would be nice to make it so instead of falling back to a random react component of our choosing from suspense, we should instead stay on the currently rendered page until React.lazy is finished. Then once that has been lazy loaded, move to that route. In other words if 'null' is specified for the suspense fallback, stay on the currently rendered page until loading is finished, then move. This feature really is needed. |
Beta Was this translation helpful? Give feedback.
-
I have created a demo repo for solving this issue specifically - Route level code splitting in React without screen flickering.
|
Beta Was this translation helpful? Give feedback.
-
I've used your implementation @HanMoeHtet with some modification in my application and got good results from it. I've gone a bit deeper and injected the transition code into the navigator as such:
The This startTransition call should be included by default in my opinion, or activatable by config for those that want to use it, wih possibly a callback before to trigger animations as we are doing now. |
Beta Was this translation helpful? Give feedback.
-
@Nickman87 @HanMoeHtet Thanks for your demonstration! Here I am sharing some thoughts about the nested routes. E.g.: export const App = () => {
return (
<StrictMode>
<BrowserRouter>
<Routes>
<Route path="/" element={<Layout />}>
<Route index element={
<Suspense fallback={<div>Loading...</div>}>
<Home />
</Suspense>
} />
<Route path="about" element={
<Suspense fallback={<div>Loading...</div>}>
<About />
</Suspense>
} />
<Route path="*" element={
<Suspense fallback={<div>Loading...</div>}>
<NotFound />
</Suspense>
} />
</Route>
</Routes>
</BrowserRouter>
</StrictMode >
);
}; The component Currently, it is impossible for Update You should wrap your |
Beta Was this translation helpful? Give feedback.
-
Here's temporary solution, which works great for me:
|
Beta Was this translation helpful? Give feedback.
-
This caused an infinite loop of remounting in my app :/ But it worked sometimes :) |
Beta Was this translation helpful? Give feedback.
-
This modified version doesnt cause a stackoverflow and does the job for me... function useConcurrentTransition() {
const location = useLocation();
const [oldLocation, setOldLocation] = useState(location);
const [, startTransition] = useTransition();
useEffect(() => {
// if the path or search params change, mark this is a navigational transition
setOldLocation(oldLocation =>
oldLocation.pathname !== location.pathname ||
oldLocation.search !== location.search
? location
: oldLocation
);
}, [location]);
useEffect(() => {
// tell concurrent mode to pause UI rendering for a moment
startTransition(() => {});
}, [oldLocation]);
return oldLocation;
} const location = useConcurrentTransition()
<App>
<Routes location={location}>
{/* routes */}
</Routes>
</App> Though this should be built into React-Router |
Beta Was this translation helpful? Give feedback.
-
This no longer works with 6.4.0, if you use An official solution would be lovely :) |
Beta Was this translation helpful? Give feedback.
-
Would be great if we can use a promise as the route element and get the loading state via useNavigation hook when the promise is resolved. Feel free to improve the idea. |
Beta Was this translation helpful? Give feedback.
-
I think we need to have a custom router and execute useTransition in the callback of I have created a sample repository |
Beta Was this translation helpful? Give feedback.
-
I'm going to convert this to a discussion so it can go through our new Open Development process. Please upvote the new Proposal if you'd like to see this considered! |
Beta Was this translation helpful? Give feedback.
-
If anyone is still looking for this, it's available with a future flag: <BrowserRouter future={{ v7_startTransition: true }}>
<Routes>{/*...*/}</Routes>
</BrowserRouter> |
Beta Was this translation helpful? Give feedback.
-
Now that v7 uses transition by default, is there a way to get the value of |
Beta Was this translation helpful? Give feedback.
-
What is the new or updated feature that you are suggesting?
Lazy loading routes is a common pattern.
Today, if users lazy load routes, they're forced to use a Suspense with a fallback, which causes a flash in their applications.
React 18 comes with useTransition which keeps the former UI static while a new UI is being prepared.
I'd like to request react-router adopt useTransition internally, so developers dont have to manually mark navigation events as transitions. Instead, react-router can automatically manage this internally, benefiting DX and UX.
Why should this feature be included?
Lazy loaded routes will no longer experience a "flash" or "glimmer" effect when they first mount. Meanwhile, apps that use lazy loading can still keep the benefits of code splitting.
Beta Was this translation helpful? Give feedback.
All reactions