shouldRevalidate
interface ShouldRevalidateFunction {
(args: ShouldRevalidateFunctionArgs): boolean;
}
interface ShouldRevalidateFunctionArgs {
currentUrl: URL;
currentParams: AgnosticDataRouteMatch["params"];
nextUrl: URL;
nextParams: AgnosticDataRouteMatch["params"];
formMethod?: Submission["formMethod"];
formAction?: Submission["formAction"];
formEncType?: Submission["formEncType"];
text?: Submission["text"];
formData?: Submission["formData"];
json?: Submission["json"];
actionResult?: any;
unstable_actionStatus?: number;
defaultShouldRevalidate: boolean;
}
This function allows you opt-out of revalidation for a route's loader as an optimization.
There are several instances where data is revalidated, keeping your UI in sync with your data automatically:
action
is called via:
<Form>
, <fetcher.Form>
, useSubmit
, or fetcher.submit
future.unstable_skipActionErrorRevalidation
flag is enabled, loaders
will not revalidate by default if the action
returns or throws a 4xx/5xx Response
shouldRevalidate
and the unstable_actionStatus
parameteruseRevalidator
If you define shouldRevalidate
on a route, it will first check the function before calling the route loader for new data. If the function returns false
, then the loader will not be called and the existing data for that loader will persist on the page.
<Route
path="meals-plans"
element={<MealPlans />}
loader={loadMealPlans}
shouldRevalidate={({ currentUrl }) => {
// only revalidate if the submission originates from
// the `/meal-plans/new` route.
return currentUrl.pathname === "/meal-plans/new";
}}
>
<Route
path="new"
element={<NewMealPlanForm />}
// `loadMealPlans` will be revalidated after
// this action...
action={createMealPlan}
/>
<Route
path=":planId/meal"
element={<Meal />}
// ...but not this one because origin the URL
// is not "/meal-plans/new"
action={updateMeal}
/>
</Route>
Note that this is only for data that has already been loaded, is currently rendered, and will continue to be rendered at the new URL. Data for new routes and fetchers at the new URL will always be fetched initially.