• To create a custom error page in Next JS, we have to define a “_error.js” in the page folder with this given syntax.
  • We have to import our own “_ error” component instead of “next/error” further to use our custom error page.

Example

                                        

import React from 'react';

class Error extends React.Component {

     static getInitialProps({ res, err }) {

        const statusCode = res ? res.statusCode : err ? err.statusCode : null;

      return { statusCode };

}

render() {

return (

<p>

   {this.props.statusCode

   ? `An error ${this.props.statusCode} occurred on server`

   : 'An error occurred on client'}

</p>

    );

  }

}

export default Error;

Suggest An Answer

No suggestions avaliable!