【发布时间】:2021-07-13 19:39:59
【问题描述】:
希望你一切顺利:)
我的 React 路由器没有通过参数响应 URL 的变化。这是 App.js:
import React, { useContext } from "react";
import { Switch, Route } from "react-router";
import NavBar from "../Navbar/Navbar";
import Homepage from "../Homepage/Homepage";
import Shops from "../Shops/Shops";
import Products from "../Products/Products";
import Games from "../Games/Games";
import Gamepage from "../Commons/Gamepage";
const App = () => {
return (
<React.Fragment>
<NavBar token={token} />
<Switch>
<Route path="/homepage" component={Homepage} />
<Route path="/shops" component={Shops} />
<Route path="/products" component={Products} />
<Route path="/games" component={Games} />
<Route
path={`/games/:id`}
render={(props) => <Gamepage {...props} />}
/>
<Route path="/" component={Homepage} />
</Switch>
</React.Fragment>
);
};
export default App;
这是游戏页面:
import React from "react";
const Gamepage = () => {
return <h1>Hello</h1>;
};
export default Gamepage;
这是链接:
<Link to="/games/euromillions">
<div className="games-list">
<h4>Euromillions</h4>
<img src={euro} alt="euro" />
</div>
</Link>
所有其他链接(如导航栏)都在工作......但是当我点击这个特定链接时,它不起作用,停留在游戏页面:/
我试过了:
- 更改参数名称;
- 将 Gamepage 组件更改为其他内容;
- 将链接标签更改为其他位置(不适用于参数);
【问题讨论】:
标签: reactjs parameters react-router