Home > Back-end >  nextjs getInitialProps() not passing props to class components
nextjs getInitialProps() not passing props to class components

Time:10-07

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

CodePudding user response:

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