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;
}
此函式允許您選擇不重新驗證路由的 loader 作為最佳化。
有幾個實例會重新驗證資料,自動讓您的 UI 與資料保持同步
action
呼叫後<Form>
、<fetcher.Form>
、useSubmit
或 fetcher.submit
future.unstable_skipActionErrorRevalidation
標記時,如果 action
傳回或引發 4xx/5xx Response
,loaders
預設不會重新驗證shouldRevalidate
和 unstable_actionStatus
參數選擇在這些情況下重新驗證useRevalidator
觸發明確重新驗證時如果在路由中定義 shouldRevalidate
,它會先檢查函式,然後再呼叫路由 loader 以取得新資料。如果函式傳回 false
,則 不會 呼叫 loader,而該 loader 的既有資料仍會保留在頁面上。
<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>
請注意,這僅適用於已載入、目前已呈示且會在新 URL 中繼續呈示的資料。新 URL 中的新路線和擷取功能的資料將始終在初始時擷取。