Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>

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

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.

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

like image 938
Sagar Kharche Avatar asked Dec 05 '18 07:12

Sagar Kharche


2 Answers

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

like image 65
Yue Qiu Avatar answered Sep 21 '22 20:09

Yue Qiu


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.

like image 39
jorbuedo Avatar answered Sep 18 '22 20:09

jorbuedo