简体   繁体   English

history.push 和 Link 更改 URL 并且如果我有多个相同组件的路由,则不重新渲染组件

[英]history.push and Link change the URL and not re render the component if I have many routes for the same component

// the routes here
<Switch>
     <Route exact path='/:page' component={Dashboard} />

      <Route exact path='/:subCateg/:categ' component={Dashboard} />
</Switch>

I want to go from the first route to the second one, if I used (history.push) or (Link) just the URL change and not reloading.我想 go 从第一条路线到第二条路线,如果我使用(history.push)或(链接)只是 URL 更改而不重新加载。

Also, the same problem if I want to go from the second route to itself but with new params.此外,如果我想 go 从第二条路线到自身但使用新参数,也会出现同样的问题。

//here I'm in Dashbord and the History also pushing to Dashboard
onClick={
        ()=>{
            history.push({pathname:`/${categ}/${subCateg}`, state: {id: subCateg.id}})
                                    // window.location.reload(false);
            }
        }

as you can see I userd "window.location.reload(false);"如您所见,我使用了“window.location.reload(false);” and it solved the loading problem, but what if I want to Goback by browser Goback button, the same problem: change URL & not relaoding.它解决了加载问题,但是如果我想通过浏览器返回按钮返回,同样的问题:更改 URL 而不是重新加载。 also I think using "window.location.reload(false);"我也认为使用“window.location.reload(false);” is not a good practice.不是一个好习惯。

`` package.json: "react-router-dom": "^5.1.2", `` package.json:“react-router-dom”:“^5.1.2”,

As you are using react-router-dom you should not be using history.push().当你使用 react-router-dom 时,你不应该使用 history.push()。 Try to use Redirect from react-router-dom instead.尝试使用 Redirect from react-router-dom 代替。

Just use a state named redirect and set it to the new route you want to go with your function:只需使用名为 redirect 的 state 并将其设置为您想要的 go 的新路由和您的 function:

onClick={() => this.setState({ redirect: 'newpath' }};

Then just put a return inside your render just like this:然后像这样在你的渲染中放一个 return:

if (this.state.redirect) {
    return <Redirect to={this.state.redirect} />;
}
return (
    ...rest of your render
);

Caveat: I'm dangerously new to React as well as much around it, but have been pulling my hair out on this for the last day myself.警告:我对 React 以及它周围的很多东西都是危险的新手,但我自己在最后一天一直在努力解决这个问题。 So there are a number of reasons this may be very bad advice.因此,这可能是非常糟糕的建议,原因有很多。 But this was my situation and how I got it to work.但这是我的情况,也是我如何让它发挥作用的。

function newSongRedirect() {
    history.push("/SongDetail/0");
    history.go(0);
}

and hook it up to my button:并将其连接到我的按钮:

<button onClick={() => newSongRedirect() } >New Song</button>

It appears that history.push adds the value to the history, but doesn't cause it to be acted on.似乎 history.push 将值添加到历史记录中,但不会导致对其进行操作。 As now the first item, history.go(0) then causes it to act on it.作为现在的第一个项目, history.go(0)然后使其对其进行操作。

I've seen some posts referencing that history.push can't be used if you're nested in a <BrowserRouter> , but in my case, attempting to replace that in my index.js just led to other issues.我已经看到一些帖子引用了 history.push 如果您嵌套在<BrowserRouter>中,则无法使用,但在我的情况下,尝试在我的 index.js 中替换它只会导致其他问题。

Also, I found this post from what looks to be part of the team working on react router.另外,我发现这篇文章是从看起来是 React 路由器团队的一员中找到的。 It seems the useHistory hook will be replaced when they get a chance.当他们有机会时,似乎 useHistory 钩子将被替换。

Heads up: The useHistory hook is a quick stopgap for a future hook that we are working on: useNavigate.注意:useHistory 钩子是我们正在研究的未来钩子的快速权宜之计:useNavigate。 useNavigate will provide an API that is more closely aligned with and will fix a few long-standing problems with using the history API directly in the router (it'll probably look a lot like @reach/router's navigate API). useNavigate 将提供一个 API,它与直接在路由器中使用历史 API 更紧密地对齐并将解决一些长期存在的问题(它可能看起来很像 @reach/router 的导航 API)。 We are providing useHistory for now to make the migration of existing code that uses the history API as painless as possible.我们现在提供 useHistory 以尽可能轻松地迁移使用历史 API 的现有代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM