Skip to main content

RouterDataProvider

The RouterDataProvider component wraps your application and manages data fetching for routes. It executes fetchers on route changes and provides data to child components.


Props

PropTypeDescription
initialStateobjectInitial data state, used to hydrate client with server data
fetcherArgsobjectArguments passed to all fetcher functions
configobjectGlobal configuration options

Basic Usage

src/js/routes/utils.js
import { RouterDataProvider } from "@tata1mg/router";
import App from "@containers/App";

export const preparedRoutes = ({ routerInitialState }) => {
return [
{
element: (
<RouterDataProvider
initialState={routerInitialState}
fetcherArgs={{}}
config={{}}
>
<App />
</RouterDataProvider>
),
children: routes,
},
];
};

With Redux Store

Pass the Redux store through fetcherArgs to make it available in fetchers:

src/js/routes/utils.js
import { RouterDataProvider } from "@tata1mg/router";
import App from "@containers/App";

export const preparedRoutes = ({ store, routerInitialState }) => {
return [
{
element: (
<RouterDataProvider
initialState={routerInitialState}
fetcherArgs={{ store }}
config={{}}
>
<App />
</RouterDataProvider>
),
children: routes,
},
];
};

Access in fetchers:

HomePage.serverFetcher = async ({ params }, { store }) => {
return store.dispatch(fetchPageData());
};