简体   繁体   English

反应路由器 v5 历史推送更新 url 但没有重新渲染组件

[英]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.

相关问题 如何在 React Router v5 中推送到历史记录? - How to push to History in React Router v5? React-Router V5更新URL,但不呈现嵌套组件 - React-router v5 updates the URL but doesn't render nested component 在组件外部的 react-router v5 中以编程方式访问历史记录(history.push 等) - Programmatic access to history (history.push etc) in react-router v5 outside of components React Router V5 HashRouter 不存储历史记录 - React Router V5 HashRouter not storing history 在这种情况下如何使用“history.push”? 反应路由器dom V5 - How Can I Use "history.push" In This Situation? react-router-dom V5 react-router-dom v5 - 链接有效,但 Redirect 和 history.push() 无效 - react-router-dom v5 - Link works, but Redirect and history.push() do not 如何在react-router v5中使用history.push传递对象? - How to pass object with history.push in react-router v5? 反应路由器 dom v5 history.push 不呈现新页面 - React router dom v5 history.push does not render new page 如果使用 history.push 访问特定路由,React-Router v5 不会重定向 - React-Router v5 not redirecting if accessing specific route using history.push React Router v5:history.push()改变地址栏,但不改变页面 - React Router v5: history.push() changes the address bar, but does not change the page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM