[id] .tsx
const Home:NextPage<any> = (props) => {
return (
<div>
{JSON.stringify(props)}
</div>
)
}
Home.getInitialProps = async (props) => {
// getting data from Database if we have an item which matched props.query.id;
const response = await axios.get('https://MY_API.com/'+props.query.id);''
// response format is like this
/*
response: {
status: 200 | 500,item: Item | undefined
}
*/
//If response has no item,I would like to show _error.tsx instead [id].tsx
return { ...props,response };
}
export default Home;
_error.tsx
const Error:NextPage<any> = (props) => {
return <div>ERROR PAGE</div>
}
export default Error;
我找到了一个解决方案,它重定向到/_error
,但是我不想更改URL
。
localhost:3000/EXIST_ID
=>显示 [id] .tsx 并保留URL
localhost:3000/NOT_EXIST_ID
=>显示 _error.tsx 并保留URL