Loader synchronization failure in nested route hierarchies
0 reputation · 09 Jun 2022, 16:58 UTC
0 reputation · 09 Jun 2022, 16:58 UTC
In React Router v6, pagination state is typically managed by extracting page indices and limits from the request.url within a route loader. This ensures that the dataset is bounded at the API level and the state remains shareable via the URL.
When a parent route manages the primary search parameters for a dataset, child routes often depend on those same parameters to filter or refine the displayed data. While a full navigation event triggers all relevant loaders, there is uncertainty regarding the behavior of nested loaders when search parameters are updated in a way that does not trigger a full route transition.
This creates a potential synchronization gap where the parent route reflects the new pagination state, but nested loaders may not re-execute as expected.
33775 reputation · 10 Jun 2022, 04:41 UTC
Yes, a search-parameter change at the parent level triggers a navigation event that re-executes loaders for all matched routes, including nested children—provided each child declares its own loader. If a child route has no loader, it simply inherits the parent's data and will not fetch independently.
pathname, search, or hash causes React Router v6 to re-run loaders for every route in the matched hierarchy.request.url (including the new search params) automatically—no extra wiring is required.// In a parent component
import { useNavigate } from 'react-router-dom';
const navigate = useNavigate();
const updatePage = (page) => {
navigate('?page=' + page + '&limit=20', { replace: true });
};
Calling navigate with updated search params and replace: true pushes a new location, triggering the normal loader cascade.
useFetcherimport { useFetcher } from 'react-router-dom';
const fetcher = useFetcher();
const refreshChild = () => {
fetcher.load('/parent/child'); // child route path
};
Use this when you want the child to refetch without changing the URL or affecting the parent's loader.
shouldRevalidate// Route config
export const shouldRevalidate = () => true; // runs on every navigation
Forces the loader to run even when its own params haven't changed. Use sparingly—it can cause unnecessary fetches.
console.log('child loader', request.url) inside the child loader.navigate or a <Link>).loader function.Does the child route export a loader function? If not, add one (even a thin wrapper that calls the API with the parent's params) to participate in the synchronization.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.