-
Recently, I started experimenting with new routing features, and one of them involves using a loader to load data. Here is the code for my project. I don't understand why the app needs to wait for the loader to end before rendering, For convenience, here is a codesandbox // ./page1/index.tsx
import { useLoaderData } from "react-router-dom";
export function Component() {
const data = useLoaderData();
return (
<div>
<h2>this is page 1</h2>
<fieldset>
<pre>
<code>{JSON.stringify(data, null, 2)}</code>
</pre>
</fieldset>
</div>
);
}
export function loader() {
return new Promise((resolve) => {
setTimeout(() => {
resolve("page 1 data");
}, 10000);
});
} // app.tsx
import { Outlet } from "react-router-dom";
import { Suspense } from "react";
export default function App() {
return (
<div className="App">
<header>App Header</header>
<hr />
<main>
{/* not working with lazy... */}
<Suspense fallback="app loading...">
<Outlet />
</Suspense>
</main>
</div>
);
} // main.tsx
import App from "./App";
import React from "react";
import ReactDOM from "react-dom/client";
import { RouterProvider, createBrowserRouter, Navigate } from "react-router-dom";
const rootElement = document.getElementById("root")!;
const root = ReactDOM.createRoot(rootElement);
const router = createBrowserRouter([
{
path: "/",
element: <App />,
children: [
{
index: true,
element: <Navigate to="page1" />
},
{
path: "page1",
lazy: () => import("./page-1")
}
]
}
]);
root.render(
<React.StrictMode>
<RouterProvider router={router} />
</React.StrictMode>
); |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
Why is this closed? |
Beta Was this translation helpful? Give feedback.
-
Sorry I forgot to fill in the reply. After some testing, I found that only the I also tested the https://codesandbox.io/s/priceless-hofstadter-jnnlsw?file=/src/index.tsx |
Beta Was this translation helpful? Give feedback.
-
Am I understanding correctly? Traditional useEffect fetching: With React Router's loader: It's just less spinner, replaced by blankscreen (or fallbackElement), but it's only improving UX (loading feeling), not the real fetching time is shortened? Maybe for deeply nested routes with waterfalls, it'd be different? |
Beta Was this translation helpful? Give feedback.
@Toshinaki
Sorry I forgot to fill in the reply.
After some testing, I found that only the
fallbackElement
property ofRouterProvider
works, which can be used to display a full-screen loading state.I also tested the
v7_startTransition
flag, but it didn't work as I thought.https://codesandbox.io/s/priceless-hofstadter-jnnlsw?file=/src/index.tsx