reactjs - NextJS - 在自定义应用程序中将状态作为道具传递给孩子
问题描述
我./pages/_app.js
从官方文档中得到了这个:
import App, { Container } from 'next/app'
class MyApp extends App {
static async getInitialProps({ Component, ctx }) {
let pageProps = {}
if (Component.getInitialProps) {
pageProps = await Component.getInitialProps(ctx)
}
return { pageProps }
}
render () {
const { Component, pageProps } = this.props
return (
<Container>
<Component {...pageProps} />
</Container>
)
}
}
export default MyApp
我想将状态从 MyApp 传递到它呈现的每个页面,但不知道如何。我试过这个:
const childProps = {
...
}
<Container>
<Component {...childProps} />
</Container>
并得到一个错误:
React.Children.only expected to receive a single React element child.
解决方案
罪魁祸首是
<Link>
<Icon />
text
</Link>
在呈现的页面中(不是_app.js
)。Link 有两个孩子(react-router Link 的残余)而不是一个。
推荐阅读
- html - bootstrap 4 pull-right 在导航栏上不起作用
- javascript - 图像不通过 jquery 显示
- php - 如何将图像文件名内爆到mysql数据库中
- excel - 对于每个循环都没有按预期运行
- node.js - 如何确保对我的服务器的请求仅通过单一来源/用户界面起作用?
- sql - 如何从此结果中删除重复项
- python - 如何保留 ufunc 和 operator xarray 操作的属性?
- javascript - 是否有一个数组函数产生的元素多于它接收的元素?
- javascript - 在具有 Javascript 和转到定义 (F12) 功能的 Visual Studio Code 中无法访问代码
- asp.net-core-webapi - 如何在 AspNet.Security.OpenIdConnect.Server (ASOS) 中使用刷新令牌获取新的访问令牌