无法读取未定义的属性'pushState' [英] Cannot read property 'pushState' of undefined
问题描述
我对React Router有这个简单的配置。我有另一个基本上用...包装,这是有效的。但是这个没有(当然我尝试使用不同的实现,如的答案中所建议的那样)这篇文章和许多其他文章。
I have this simple configuration for the React Router. I have another one with basically wrapping with ... , which works. But this one doesn't (of course I tried to use with different implementations like suggested in the answers of this post and many others.
控制台错误是这篇文章的标题。
使用ES6和react-router v.1与基于散列的路由。
The console error is the title of this post. Using ES6, and react-router v.1 with hash-based routing.
我读了很多的文章,对于简单的路由实施来说太不必要了,而且几乎厌恶做出反应和反应 - 路由器现在。请帮忙。
I read a lot of articles, too unnecessary for simple routing to be implemented, and almost hating react and react-router now. Please help.
componentWillReceiveProps() {
this.contextTypes = {
history: React.PropTypes.object
}
},
_handleRoute(e) {
e.preventDefault()
this.history.pushState(null, `/somepath`);
},
render() {
return(
<div onClick={this._handleRoute}>
Some Content.
</div>
);
}
或:
render() {
return(
<div>
<Link to={`/somepath`}> link </Link>
</div>
);
}
推荐答案
React Router v1 :
历史对象中有pushState方法。
There's the pushState method in history object made for this.
首先分配的组件对于路由器必须给子组件pushState方法作为函数prop:
First the component assigned for router has to give the child component the pushState method as a function prop:
//Router Component
render ()
return (
<SomeComponent changeRoute={this.props.history.pushState}
)
然后,在我要在函数中更改路径的Component中,我只是这样做:
Then, in the Component that I want to change route in a function, I simply do:
//SomeComponent
onClick() {
this.props.changeRoute(null, 'somepath', {query: "something"});
}
React Router v.2
import {browserHistory} from 'react-router';
//SomeComponent
browserHistory.push('somepath');
这篇关于无法读取未定义的属性'pushState'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!