Skip to content

refresh

refresh 允许你在 服务器动作 中刷新客户端路由。

¥refresh allows you to refresh the client router from within a Server Action.

用法

¥Usage

refresh 只能在服务器操作中调用。它不能用于路由处理程序、客户端组件或任何其他上下文中。

¥refresh can only be called from within Server Actions. It cannot be used in Route Handlers, Client Components, or any other context.

参数

¥Parameters

tsx
refresh(): void;

返回

¥Returns

refresh 不返回值。

¥refresh does not return a value.

示例

¥Examples

ts
'use server'

import { refresh } from 'next/cache'

export async function createPost(formData: FormData) {
  const title = formData.get('title')
  const content = formData.get('content')

  // Create the post in your database
  const post = await db.post.create({
    data: { title, content },
  })

  refresh()
}

在服务器操作之外使用时的错误

¥Error when used outside Server Actions

ts
import { refresh } from 'next/cache'

export async function POST() {
  // This will throw an error
  refresh()
}