React-Router:为什么推送到历史会改变我的 url?

React-Router: Why does pushing to history change my url?

我正在尝试利用 createMemoryHistory 在不更改 url 地址的情况下四处移动,因为我的应用程序将在 iframe 中呈现。但是,当我推送到 history 时,它似乎更新了我的 url。任何提示将不胜感激!

//history.js
import createMemoryHistory from "history/createMemoryHistory";
const history = createMemoryHistory();
export default history;

//App.js
import history from './history/history';
...
<Router>
   <Route
      path={'/'}
      render={(props) => <Component {...props}/>}
   />
</Router>

//component.js
...
function handleClick(history) {
    history.push('somePath'); // this updates my url to be url.com/somePath
} 
return (<Button onClick={() => handleClick(this.props.history)}>);

问题已解决,我导入的东西不正确哈哈。最后仍然坚持 createMemoryHistory 而不是 createBrowserHistory

在使用MemoryHistory时,应该将历史对象传递给Router,导入创建的历史后直接使用,如

App.js

import history from './history/history';
...
<Router history={history}>
   <Route
      path={'/'}
      render={(props) => <Component {...props}/>}
   />
</Router>

component.js

import history from './history/history';
...
function handleClick() {
    history.push('somePath'); // this updates my url to be url.com/somePath
} 
return (<Button onClick={() => handleClick()}>);