[英]How to pass props to layout.js from page.js in Next.js 13?
I want to pass props
to layout.js
of Next.js 13. Here's what it'd look like:我想将
props
传递给layout.js
13 的 layout.js。这是它的样子:
// layout.js
export default Layout({children}) {
return (
<>
{/* I want to render different `text` depending on the page.js I'm rendering */}
<h1>{text}</h1>
{children}
</>
);
}
Unfortunately, I end up doing this:不幸的是,我最终这样做了:
// CustomLayout.js
export default Layout({children, text}) {
return (
<>
{/* I want to have different `text` depending on the Page.js I'm rendering */}
<h1>{text}</h1>
{children}
</>
);
}
My question is, is it possible to pass props to layouts in Next.js 13?我的问题是,是否可以将道具传递给 Next.js 13 中的布局? Is there a better approach?
有更好的方法吗?
// In every page.js
import Layout from "somewhere/CustomLayout";
export default Page() {
return (
<Layout text="My text">
{/* My Page Content */}
</Layout>
);
}
You shouldn't be trying to pass custom props
to Layout
, as this component is created by you, but Next.js itself would call it, by giving it two props
:你不应该试图将自定义
props
传递给Layout
,因为这个组件是由你创建的,但 Next.js 本身会通过给它两个props
来调用它:
children (required)
孩子(必填)
Layout components should accept and use a
children
prop.布局组件应该接受并使用
children
道具。 During rendering, children will be populated with the route segments the layout is wrapping.在渲染期间,子项将填充布局环绕的路线段。 These will primarily be the component of a child Layout (if it exists) or Page, but could also be other special files like Loading or Error when applicable.
这些将主要是子布局(如果存在)或页面的组件,但也可能是其他特殊文件,如适用时加载或错误。
params (optional)
参数(可选)
The dynamic route parameters object from the root segment down to that layout.
从根段向下到该布局的动态路由参数 object。
Wanting to pass custom props
to it, I think, is because you wanna use it in different places, but the whole idea is to have one Layout
by route segment.想要将自定义
props
传递给它,我认为是因为您想在不同的地方使用它,但整个想法是按路线段进行Layout
。 And subroutes inherit the layout of outer ones.并且子路由继承了外部路由的布局。 For example,
layout.js
inside app/blog
gets wrapped by layout.js
inside app/
.例如,
app/blog
中的layout.js
被app/
中的layout.js
包裹。
Having Layout
called inside page.js
as you did, is like having any wrapper, but not a Layout
as designed by Next.js with its specific abilities:像您一样在
page.js
中调用Layout
就像拥有任何包装器,但不是由 Next.js 设计的具有特定功能的Layout
:
A layout is UI that is shared between multiple pages.
布局是在多个页面之间共享的 UI。 On navigation, layouts preserve state, remain interactive, and do not re-render.
在导航中,布局保留 state,保持交互,并且不重新渲染。 Layouts can also be nested.
布局也可以嵌套。
import Layout from "somewhere/CustomLayout";
export default Page() {
return (
// ⚠️ this would behave like a normal wrapper, but not as a Layout as Next.js defines it
<Layout text="My text">
{/* My Page Content */}
</Layout>
);
}
No, you shouldn't pass in additional props to a layout.js file in nextjs 13 appdir.不,你不应该将额外的道具传递给 nextjs 13 appdir 中的 layout.js 文件。
As @yousoumar pointed out, you shouldn't be calling Layout yourself.正如@yousoumar 指出的那样,您不应该自己调用 Layout。 It sits at the root (at least for all the components under it), so should be treated as such.
它位于根目录(至少对于它下面的所有组件而言),因此应该这样对待。
Your approach of creating a custom layout that passes text as a prop is a reasonable approach.您创建将文本作为道具传递的自定义布局的方法是一种合理的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.