【问题标题】:nextjs getInitialProps() not passing props to class componentsnextjs getInitialProps() 没有将道具传递给类组件
【发布时间】:2021-10-06 14:33:24
【问题描述】:

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

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' 上获取 undeifend 并且 'h1' 是 emtpy

【问题讨论】:

标签: reactjs next.js


【解决方案1】:

你应该 return 组件的 props 而不是将它放在另一个 props 对象中。 Read the Docs

static async getInitialProps(ctx) {
  const test = 'test 123';
  return { test };
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 2021-03-22
    • 2019-02-26
    • 1970-01-01
    • 2019-04-24
    • 1970-01-01
    相关资源
    最近更新 更多