refresh
refresh 允許您從伺服器操作中重新整理客戶端路由器。
用法
refresh 只能從伺服器操作中呼叫。它不能在路由處理程式、客戶端元件或任何其他上下文中使用。
引數
refresh(): void;返回
refresh 不返回任何值。
示例
app/actions.ts
'use server'
import { refresh } from 'next/cache'
import { redirect } from 'next/navigation'
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()
}在伺服器操作之外使用時出錯
app/api/posts/route.ts
import { refresh } from 'next/cache'
export async function POST() {
// This will throw an error
refresh()
}這有幫助嗎?