Created
November 4, 2018 14:41
-
-
Save mkamakura/3f4742f160f7add2c5016b61b0bb0d17 to your computer and use it in GitHub Desktop.
withError on Nextjs
This file contains 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' | |
import ErrorPage from 'next/error' | |
export default Component => { | |
return class WithError extends React.Component { | |
static async getInitialProps(ctx) { | |
const props = | |
(Component.getInitialProps | |
? await Component.getInitialProps(ctx) | |
: null) || {} | |
if (props.statusCode && ctx.res) { | |
ctx.res.statusCode = props.statusCode | |
} | |
return props | |
} | |
render() { | |
if (this.props.statusCode) { | |
return <ErrorPage statusCode={this.props.statusCode} /> | |
} | |
return <Component {...this.props} /> | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment