面试题答案
一键面试设置404页面步骤
- 在
pages
目录下创建404.js
文件。 - 在
404.js
中编写404页面的UI和逻辑。例如:
import React from 'react';
const Custom404 = () => {
return (
<div>
<h1>404 - Page Not Found</h1>
<p>The page you are looking for does not exist.</p>
</div>
);
};
export default Custom404;
自定义路由规则示例
- 在
pages
目录下创建products
目录。 - 在
products
目录下创建[id].js
文件。这个[id]
就是动态路由参数。 - 在
[id].js
中编写页面组件逻辑,例如:
import React from 'react';
const ProductPage = ({ query }) => {
const { id } = query;
return (
<div>
<h1>Product Page - {id}</h1>
<p>This is the page for product with ID: {id}</p>
</div>
);
};
export default ProductPage;
这样,当访问 /products/123
时,就会渲染 ProductPage
组件,并把 id
参数传递进去。