Skip to content

Instantly share code, notes, and snippets.

@codeape2
Created October 5, 2022 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeape2/851f31ae5ffab4ca8c72d1cc85d17086 to your computer and use it in GitHub Desktop.
Save codeape2/851f31ae5ffab4ca8c72d1cc85d17086 to your computer and use it in GitHub Desktop.
Example application for the route loader behaviour bug
import {
createBrowserRouter,
Outlet,
Link,
RouterProvider,
useLoaderData,
useNavigate
} from 'react-router-dom';
function recordLoaderInvocation(routeName) {
console.log(" loader(", routeName, ")");
return routeName;
}
function useNavigateWithConsoleLog() {
const navigate = useNavigate();
return (args) => {
console.log("navigate(", args, ") >");
navigate(args);
}
}
function RootRoute() {
const navigate = useNavigateWithConsoleLog();
return <div>
<h1>Root</h1>
<button onClick={()=>navigate({pathname: "children/1"})}>Child1</button>
<button onClick={()=>navigate({pathname: "children/2"})}>Child2</button>
<button onClick={()=>navigate({pathname: "children/1", search: "?foo=bar"})}>Child1 w/ search</button>
<button onClick={()=>navigate({pathname: "children/2", search: "?foo=bar"})}>Child2 w/ search</button>
<Outlet/>
</div>;
}
const routes = [
{
path: "/",
loader: () => recordLoaderInvocation("root"),
element: <RootRoute/>,
children: [
{
path: "children",
loader: () => recordLoaderInvocation("children"),
element: <>
<h2>Children</h2>
<Link to="..">back to root</Link>
<Outlet/>
</>,
children: [
{
path: ":childid",
loader: ({params}) => recordLoaderInvocation(`child ${params.childid}`),
element: <ChildRoute/>
}
]
}
]
}
]
function ChildRoute() {
return <h3>I am {useLoaderData()}</h3>
}
function App() {
return <RouterProvider router={createBrowserRouter(routes)}/>
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment