From 7765c48ffbbbb8fc5c0c2cb74828c64b9d0bd35a Mon Sep 17 00:00:00 2001 From: Rikako Wu <496063163@qq.com> Date: Fri, 2 Jun 2023 21:14:39 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=85=E8=AF=BBnext.js=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- react/next.js.md | 126 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) 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