reactjsnext.jshigh-order-component

In NextJS, how do i add getInitialProps() to a HOC-wrapped-functional-element?


I have a HOC-wrapped-functional-component here

export default wrapperHoc( function myComponent ({ someProps }){
    return(
       <div/>
    )
})

How do i write getInitialProps for myComponent ?

Should i call myComponent's getInitialProps in wrapperHoc?


Solution

  •   const YourNewComponent = wrapperHoc(...)
    
      YourNewComponent.getInitialProps = async (ctx) => {
        const res = await fetch('https://api.github.com/repos/vercel/next.js')
        const json = await res.json()
        return { stars: json.stargazers_count }
      }
    
      export default YourNewComponent