clientAction
除了(或代替)action
之外,你还可以定义一个将在客户端执行的 clientAction
函数。
¥In addition to (or in place of) your action
, you may define a clientAction
function that will execute on the client.
每个路由都可以定义一个 clientAction
函数来处理变更:
¥Each route can define a clientAction
function that handles mutations:
export const clientAction = async ({
request,
params,
serverAction,
}: ClientActionFunctionArgs) => {
invalidateClientSideCache();
const data = await serverAction();
return data;
};
此函数仅在客户端运行,并且可以通过以下几种方式使用:
¥This function is only ever run on the client and can be used in a few ways:
对于全客户端路由,不再使用服务器 action
¥Instead of a server action
for full-client routes
要通过在变更时使缓存无效来与 clientLoader
缓存一起使用
¥To use alongside a clientLoader
cache by invalidating the cache on mutations
为了方便从 React Router 迁移
¥To facilitate a migration from React Router
¥Arguments
params
¥This function receives the same params
argument as an action
.
request
¥This function receives the same request
argument as an action
.
serverAction
serverAction
是一个异步函数,它为此路由向服务器 action
发出 fetch 调用。
¥serverAction
is an asynchronous function that makes the fetch call to the server action
for this route.
另请参阅:
¥See also: