clientAction
On this page

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

此函数接收与 action 相同的 params 参数。

¥This function receives the same params argument as an action.

request

此函数接收与 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:

Remix v2.17 中文网 - 粤ICP备13048890号