如何在反应中更改历史对象的状态?



我试图修改当前历史对象的条目而不是创建一个新的条目,但我收到此错误

历史.替换不是一个函数

错误屏幕截图

//My attempts were:
import { Switch, Route, Redirect } from 'react-router-dom';

history.replaceState( position );
history.replace( null, position );

我正在使用 react-router-dom 的版本 ^16.13.1"。

react-router使用history包,因此您必须导入它。

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

使用vanilla Javascript,如果您只想更新URL而不重新加载任何组件,则可以使用window.history.replaceState({}, title, position)

最新更新