如何在反应路由器中更新 history.replace 上的路由器状态? [英] How to update the router state on history.replace in react router?

查看:38
本文介绍了如何在反应路由器中更新 history.replace 上的路由器状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:-

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

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

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

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

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

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.

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

有人知道我做错了什么吗?为什么在使用 history.replace

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

推荐答案

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

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.replace 上的路由器状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆