diff --git a/react/next.js.md b/react/next.js.md index 2871850..275ff82 100644 --- a/react/next.js.md +++ b/react/next.js.md @@ -68,3 +68,129 @@ root layout使用需要满足如下条件 - root layout必须要定义``标签和`
`标签,next.js并不会自动创建它们 - 如果layout.js存在多层嵌套,例如`/layout.js`和`/about/layout.js`,`/about/asahi/layout.js`都存在,那么嵌套结构将是layout.js->about/layout.js->about/asahi/layout.js->about/asahi/page.js,其中about/asahi/page.js渲染的效果将会包含上层所有的layout +#### template +template和layout相似,在template中也可以包含子page和子layout。 + + +layout和template区别如下: +- layout在route跳转时,layout本身维护的state会保持不变 +- 而template在route跳转时,**对于其每一个children,都会含有一个独立的template实例**,template在多个route进行跳转时,state不会持久化 + +> 故而,只有在layout中维护的state才会保持不变,而template中维护的state,在route之间进行跳转时,会清空 + +可以通过创建一个template.js文件并且在文件中export default一个组件来定义template +```js +export default function Template({ children }) { + return