nextjs getInitialProps()不向类组件传递道具



我得到空结果。我做错了什么?

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 };
}

最新更新