[英]React router v5 history push updates url but no rerender of component
I am using history from react-router-dom v5 this works on home page, on the inside pages the history push does not work.我正在使用 react-router-dom v5 的历史,这在主页上有效,在内部页面上,历史推送不起作用。
The URL in the browser is update, but the component or page is refreshed浏览器中的URL更新了,但是刷新了组件或页面
ListComponent.js ListComponent.js
import { useHistory } from "react-router-dom";
export const ListComponent () => {
const history = useHistory();
const handleClick = () => {
history.push("/list/view-all");
}
return(
<button onClick={handleClick}>View All List</button>
);
}
ListViewAllComponent.js ListViewAllComponent.js
import { useHistory } from "react-router-dom";
import { ListComponent } from './ListComponent';
export const ListViewAllComponent() => {
return(
...
some other html codes
...
<ListComponent />
);
}
app.js应用程序.js
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
function App() {
return (
<Router>
<Switch>
.....
<Route exact path="/" component={ComponentName} />
....
</Switch>
</Router>
history.push () must be written in a function. history.push() 必须写在 function 中。
useEffect(() => {
history.push("/name");
}, [])
Or you can use the <Redirect to = "/" />
component或者你可以使用
<Redirect to = "/" />
组件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.