I have a dynamic route in my NextJS app /team/[id]
…. When the page loads it goes off to the API and checks the ID of the team and then returns the information, if the ID doesn’t exist on the API, it returns a 404. Is there any way to to show a 404 if the data returned back from the API is also a 404? .. At the moment, i have a page loader which runs when loading, but when the ID doesn’t exist, the pageloader just keeps going and doesn’t stop.
This is what I currently have which works fine for ID’s which exist, but breaks if an id doesnt exist;
import useSWR from "swr"
import axios from '@/lib/axios'
import { useRouter } from 'next/router'
const ViewTeam = () => {
const router = useRouter()
const { id } = router.query
const { data: team, error, mutate } = useSWR('/api/team/' + id, () =>
axios.get('/api/team/' + id)
.then(response => response.data.data)
)
if (! team) {
return (<PageLoader />)
}
return (
//
)
}
3
Answers
Usually you should have something like
isLoading
orloading
state which will show your<PageLoader />
component. InuseSWR
docs I see it isisLoading
. So you just do:and after that depending on your request response you define what should happen when it gets the data (correct or wrong).
If it’s wrong it should be for example:
and when it’s correct you just show rest:
so finally it should look something like:
You should ideally fetch data inside useEffect. By using useEffect with proper dependency management, you ensure that data fetching and any associated cleanup happen at the appropriate times, leading to a more predictable and efficient React application.
using [ ] ensures you will only fetch once on mount.
Inside return() you should check if fetching was completed if not show loading page
If you dont want your url to be changed when
team
is not found but want to show same 404 page, then you can do something like this.