Using React Router
In this section, we will learn about how React Router can integrate with the data-fetching process. We will use this knowledge to simplify the code that fetches blog posts in our app.
Understanding React Router data loading
React Router data loading is similar to React Router forms, which we learned about in Chapter 7. Instead of defining an action that handles form submission, we define a loader that handles data loading. The following code snippet defines a loader on a some-page
route:
const router = createBrowserRouter([ ..., { path: '/some-page', element: <SomePage />, loader: async () => { const response = fetch('https://somewhere'); return await response.json(); } }, ... ]);
React Router calls the loader...