Skip to content

Instantly share code, notes, and snippets.

@mkamakura
Created November 4, 2018 14:41
Show Gist options
  • Save mkamakura/3f4742f160f7add2c5016b61b0bb0d17 to your computer and use it in GitHub Desktop.
Save mkamakura/3f4742f160f7add2c5016b61b0bb0d17 to your computer and use it in GitHub Desktop.
withError on Nextjs
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