I am working on implementing a new React Router. I am using the new createBrowserRouter
and want to implement some loader
API calls. I have my project set up to make these API calls properly, however I can’t find any docs on how to actually handle this data.
I want to pass the response data down to my component but it isn’t clear how I do this.
I have an API folder with this network request.
export function getReasonTypes() {
commonAxios
.get('/api/ReasonTypes?activeOnly=true')
.then(function (response) {
console.log(response)
return response.data
})
.catch(function (error) {
console.log(error)
})
}
I import this function when I create my router:
function defaulNewAPIs() {
const data = getReasonTypes()
return { data }
}
const router = createBrowserRouter([
{
path: '/:appTypeId?',
element: <AppLayout />,
loader: defaulNewAPIs,
children: [
{
index: true,
element: <ReferralDetails status="new" />,
},
],
},
])
ReactDOM.createRoot(document.getElementById('root')!).render(
<GenReMsal>
<React.StrictMode>
<AuthenticatedTemplate>
<RouterProvider router={router} />
</AuthenticatedTemplate>
<UnauthenticatedTemplate>
<GenReLogin />
</UnauthenticatedTemplate>
</React.StrictMode>
</GenReMsal>
)
The above code successfully calls my api, and based on the console log I can see it fetches data correctly. How do I pass this data into my router elements?
2
Answers
Your almost done all you need to do in the AppLayout component is call the react router useLoaderData hook. react router docs
The
getReasonTypes
function needs to return a value for thedefaulNewAPIs
route loader function to return to the route component.Return the Promise chain
Use
async/await
The
defaulNewAPIs
loader function should wait for the resolved value returned by thegetReasonTypes
function.The component that you want to read the loaded route state should use either the useLoaderData
or useRouteLoaderData hooks.