react-router v4-browserHistory未定义 [英] react-router v4 - browserHistory is undefined
问题描述
我正在电子中创建我的第一个反应应用程序(也是我的第一个电子应用程序)。我有两条路线需要从一个导航到另一个。为此,我使用以下代码:
I am creating my first react app in electron (my first electron app too). I have two routes & need to navigate from one to another. For that I am using following code:
根
ReactDOM.render(
<Router history={browserHistory}>
<App />
</Router>,
document.getElementById('root')
);
应用
class App extends React.Component {
constructor() {
super();
}
render() {
return (
<div className="app-master">
<Switch>
<Route path='/city' component={CityList}/>
<Route path='/' component={SplashScreen}/>
</Switch>
</div>
)
}
}
页面
import { browserHistory } from 'react-router';
...
browserHistory.push('/city');
此行显示错误,
TypeError: Cannot read property 'push' of undefined
的属性 push寻找可能的解决方案,但找不到一个! SO上也有许多类似的问题,但是对我来说都不起作用:(
I searched web for possible solution but can't find one! There are many similar questions on SO too, but none of it worked for me :(
推荐答案
您必须从历史记录模块现在提供了三种创建不同历史记录的方法。
You have to import it from the history module now which provides 3 different methods to create different histories.
-
createBrowserHistory
用于支持历史API -
createMemoryHistory
用作参考实现,也可能可以在非DOM环境中使用,例如React Native或测试 -
createHashHistory
用于旧版Web浏览器
createBrowserHistory
is for use in modern web browsers that support the history APIcreateMemoryHistory
is used as a reference implementation and may also be used in non-DOM environments, like React Native or testscreateHashHistory
for legacy web browsers
您不能在电子环境中使用浏览器历史记录,而不能使用哈希或内存。
You cannot use the browser history in an electron environment, use the hash or the memory one.
import { createHashHistory } from 'history'
const history = createHashHistory()
您可以使用历史记录
注入道具
this.props.history.push('/')
这篇关于react-router v4-browserHistory未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!