简体   繁体   中英

How to change the state of history object in react?

I was trying to modify the entry of the current history object instead of creating a new one but I got this error

history.replace is not a function

Error screenshot

//My attempts were:

import { Switch, Route, Redirect } from 'react-router-dom';


   history.replaceState( position );

   history.replace( null, position );

I'm using version ^16.13.1" of react-router-dom.

react-router uses the history package, so you have to import it .

import { createBrowserHistory } from 'history';
const history = createBrowserHistory();
const position = '/logged-in';
history.replace(position);

With vanilla Javascript, if you just want to update the URL without reloading any components, you can use window.history.replaceState({}, title, position) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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