简体   繁体   中英

nextjs getInitialProps() not passing props to class components

im getting empty results. what im i doing wrong?

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

getting undeifend on the 'console.log' and the 'h1' is emtpy

You should return the component's props without putting it in another props object. Read the Docs

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM