app/template.tsx
from Special Files
#WIP
https://zenn.dev/cybozu_frontend/articles/8caf1decb1e82c
layout.tsxとの違い
layoutの方はページ感でstteを維持するが、templateの方はページを移動する毎にsteteをリセットする
https://beta.nextjs.org/docs/routing/pages-and-layouts#templates
オプションのファイルで、レイアウトと似ていますが、ナビゲーション時にコンポーネントの新しいインスタンスがマウントされ、ステートは共有されません。入退場時のアニメーションなど、この動作が必要な場合にテンプレートを使用することができます。
意味不
状態保持市内版のlayout.tsxってこと #??
navigationした時に、毎回新しいinstanceを作るので状態は共有されない
layout.tsxとtemplate.tsxが併存しているときの扱いとかわからない
Layoutの子になるらしい
code:tsx
<Layout>
{/* Note that the template is given a unique key. */}
<Template key={routeParam}>{children}</Template>
</Layout>
ユースケース
vercel/app-playgroundを触るではtemplate.tsxの中身はほぼこれ
code:tsx
export default function Template({ children }: { children: React.ReactNode }) {
return <Boundary>{children}</Boundary>;
}
Boundaryをlayout.tsxに書くか、template.tsxに書くかの違い
layoutの方に書くと、初回ロード時のみfallbackする
templateの方に書くと、navigationするたびにfallbackする
なんとなくわかったmrsekut.icon