I want to fetch data from an API, and if it fails I want it to render the _error.js
page. My request depends on the router query. So if the user enters a wrong query the page is going to throw an error.
If the request fails by throwing, I want my custom _error.js
to be displayed.
How do I achieve this?
Here is my index page:
// pages/index.js
import React from 'react'
import PropTypes from 'prop-types'
import fetch from 'isomorphic-unfetch'
import Items from '../components/Items'
import { API } from '../utils/config'
const IndexPage = ({ items }) => {
return (
<div>
<Items items={items} />
</div>
)
}
IndexPage.getInitialProps = async (context) => {
const { query } = context
const filter = query.filter || ''
const name = query.name || ''
const page = query.page || 1
const response = await fetch(
`${API}?${filter}=${name}&page=${page}`,
)
const data = await response.json()
return {
items: data.items,
}
}
export default IndexPage
my _app file:
// pages/_app.js
import React from 'react'
import Router from 'next/router'
import Page from '../components/Page'
const MyApp = (props) => {
const { Component, pageProps } = props
return (
<Page>
<Component
items={pageProps.items}
query={pageProps.query}
statusCode={pageProps.statusCode}
/>
</Page>
)
}
export default MyApp
Handling Server Errorsjs provides a static 500 page by default to handle server-side errors that occur in your application. You can also customize this page by creating a pages/500. js file. Having a 500 page in your application does not show specific errors to the app user.
Make sure the returned object from getInitialProps is a plain Object and not using Date , Map or Set . For the initial page load, getInitialProps will run on the server only. getInitialProps will then run on the client when navigating to a different route via the next/link component or by using next/router .
Addition of getServerSideProp and getServerSideProp is greatly desirable, since getInitialProps will be deprecated and most likely deleted from next.
In order to use Error Boundary in Functional Component, I use react-error-boundary. When we run this application, we will get a nice error display form the content of ErrorHandler component. React error boundary catches any error from the components below them in the tree.
Try Conditional rendering based on the error, so if in an error occurs set the items
to null
const IndexPage = ({ items }) => {
return (
{items===null ? <Fragment>
<ErrorPage />
</Fragment> : <div>
<Items items={items} />
</div> }
)
}
and in the getInitialProps
IndexPage.getInitialProps = async (context) => {
try{
const { query } = context
const filter = query.filter || ''
const name = query.name || ''
const page = query.page || 1
const response = await fetch(
`${API}?${filter}=${name}&page=${page}`,
)
const data = await response.json()
return {
items: data.items,
}
}catch(error){
return {
items: null
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With