javascript 如何在反应路由器中更新 history.replace 上的路由器状态?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53627453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 10:15:21  来源:igfitidea点击:

How to update the router state on history.replace in react router?

javascriptreactjsreact-router

提问by Sagar Kharche

My react router has state isActive(boolean)value. When I use the Link component to redirect to different route I can update the state as follows and its working fine:-

我的反应路由器具有状态isActive(boolean)值。当我使用 Link 组件重定向到不同的路由时,我可以按如下方式更新状态并且它工作正常:-

<Link to={{
        pathname: "my-home-page",
        search: '?query=abc',
        state: { isActive: true }
}}>Go to Home</Link>

Also when I use history.push state is updating correctly bu using below code:-

此外,当我使用 history.push 状态正在使用以下代码正确更新时:-

history.push({
               pathname: '/template',
               search: '?query=abc', 
               state: {
                isActive: true
               }
});

However when I am using history.replace in javascript I am not able to update the state. I am trying the below code however its not working.

但是,当我在 javascript 中使用 history.replace 时,我无法更新状态。我正在尝试下面的代码,但它不起作用。

history.replace({ pathname: 'home', search: '?query=abc', isActive: true});

Anyone knows what I am doing wrong? Why my state is not updating while redirecting with history.replace

有谁知道我做错了什么?为什么我的状态在重定向时没有更新history.replace

回答by jorbuedo

What version of the router is it? In History API, and when I've used it, the first parameter is the pathname (without object, just the string), and the second parameter is the state.

这是什么版本的路由器?在History API 中,当我使用它时,第一个参数是路径名(没有对象,只有字符串),第二个参数是状态。