Created
September 24, 2019 02:58
-
-
Save deltorosalazar/f7c16c6ae96c59eaf8c3da47c131b3fe to your computer and use it in GitHub Desktop.
Next.js - getInitialProps from a HOC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
/* | |
* Higher order component that passes `getInitialProps` through | |
* to the child component | |
*/ | |
const HOC = function(Child) { | |
return class Higher extends React.Component { | |
static getInitialProps(ctx) { | |
return Child.getInitialProps(ctx) | |
} | |
render() { | |
return <Child {...this.props} /> | |
} | |
} | |
} | |
/* | |
* Child component | |
*/ | |
const class MyComponent extends React.Component { | |
static getIntialProps({ req }) { | |
return {prop: "you want to return"} | |
} | |
render() { | |
return <h1>Hello, World</h1> | |
} | |
} | |
export default HOC(MyComponent) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment