跳至內容

template.js

樣板檔案類似於佈局,它會包裝佈局或頁面。與跨路由持續存在並維持狀態的佈局不同,樣板會被賦予唯一鍵值,這表示子客戶端組件會在導覽時重設其狀態。

app/template.tsx
export default function Template({ children }: { children: React.ReactNode }) {
  return <div>{children}</div>
}
template.js special file

雖然比較少見,但如果您想要使用以下功能,您可以選擇使用樣板而不是佈局:

  • 依賴 useEffect 的功能(例如記錄頁面瀏覽量)和 useState(例如每頁的回饋表單)。
  • 更改預設框架行為。例如,佈局內的 Suspense Boundaries 只會在佈局首次載入時顯示 fallback,而切換頁面時則不會顯示。對於模板,每次導覽都會顯示 fallback。

屬性

children(必要)
輸出
<Layout>
  {/* Note that the template is automatically given a unique key. */}
  <Template key={routeParam}>{children}</Template>
</Layout>

注意事項:

版本歷史記錄

版本變更
v13.0.0引入 template