Created
October 5, 2022 12:52
-
-
Save codeape2/851f31ae5ffab4ca8c72d1cc85d17086 to your computer and use it in GitHub Desktop.
Example application for the route loader behaviour bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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