我得到空结果。我做错了什么?
indx.js:
export default class Home extends React.Component {
static async getInitialProps(ctx) {
const test = 'test 123'
return {props: {test: test}}
}
render() {
console.log(this.props.test)
return (
<>
<h1>{this.props.test}</h1>
</>
)
}
}
_app.js:
function MyApp({ Component, pageProps }) {
return (
<Component {...pageProps} />
)
}
export default MyApp
在'console.log'上得到undefifend, 'h1'为空
你应该return
组件的道具,而不是把它放在另一个props
对象。Read the Docs
static async getInitialProps(ctx) {
const test = 'test 123';
return { test };
}