admin管理员组文章数量:1178528
i am quite new to react and react-router.
Problem with react is here that, on some pages of my application react-router is working and some giving error like this: "Cannot read property 'push' of undefined".
i am using react 0.14.1
My routing code looks like this:
render(
<Router history={hashHistory}>
<Route path="/" component={Loginpanel}/>
<Route path="Index" component={Index}/>
<Route path="Form" component={Form} />
<Route path="Checkindex" component={Index}/>
<Route path="Signup" component={Signup}/>
<Route path="Admin" component={Admin}/>
<Route path="AdminEditDetails" component={AdminEditDetails}/>
<Route path="AdminDetails" component={AdminDetails}/>
</Router>,
document.getElementById('js-main'));
My component is like this now:
class App extends React.Component {
constructor(props) {
super(props);
this.state= {
comments: AppStore.getAll();
};
}
componentWillMount() {
var length = this.statements.length;
var firstnumber = length - 4;
if(length == 0){
console.log("here comes");
this.props.router.push('/');
}
else{
console.log('work normally');
}
}
}
Can anyone help me with this? Thanks.
i am quite new to react and react-router.
Problem with react is here that, on some pages of my application react-router is working and some giving error like this: "Cannot read property 'push' of undefined".
i am using react 0.14.1
My routing code looks like this:
render(
<Router history={hashHistory}>
<Route path="/" component={Loginpanel}/>
<Route path="Index" component={Index}/>
<Route path="Form" component={Form} />
<Route path="Checkindex" component={Index}/>
<Route path="Signup" component={Signup}/>
<Route path="Admin" component={Admin}/>
<Route path="AdminEditDetails" component={AdminEditDetails}/>
<Route path="AdminDetails" component={AdminDetails}/>
</Router>,
document.getElementById('js-main'));
My component is like this now:
class App extends React.Component {
constructor(props) {
super(props);
this.state= {
comments: AppStore.getAll();
};
}
componentWillMount() {
var length = this.state.comments.length;
var firstnumber = length - 4;
if(length == 0){
console.log("here comes");
this.props.router.push('/');
}
else{
console.log('work normally');
}
}
}
Can anyone help me with this? Thanks.
Share Improve this question edited Aug 28, 2016 at 1:26 Nader Dabit 53.7k14 gold badges109 silver badges91 bronze badges asked Aug 27, 2016 at 17:46 user6730596user67305967 Answers
Reset to default 15Using hashHistory
directly from react-router
is certainly an option, but it makes unit-testing a bit more painful.
Browser history will generally not be available in the context tests run in and it's easier to mock out a prop than to mock out a global API.
Consider using the withRouter
higher-order-component that react-router
provide (since v2.4.0
).
import { withRouter } from 'react-router';
class App extends Component {
(...)
}
export default withRouter(App);
Your App
class will receiver router
via props and you can safely do this.props.router.push('/');
.
If you are use Partial Component and calling it like <Header />
then you should replace it by <Header {...this.props} />
Your app does not have an instance of Router in its props which is giving you Cannot read property 'push' of undefined
.
I'm assuming you're importing withRouter to get the instance of the Router so you'd need to wrap the component in that if you still want to use that... (example here but not suggested)
Instead, a better approach to programmatically navigating is to use
import { hashHistory } from 'react-router;'
...
hashHistory.push('/');
in your componentWillMount
lifecycle event.
The docs are here
Hope this helps!
You are using hashHistory. The following code should work.
import { hashHistory } from 'react-router';
hashHistory.push('/');
The root route should also be defined.
<Route path="/" component={App}>
you can try the below code see if your router props
is working or not
componentWillMount() {
var length = this.state.comments.length;
var firstnumber = length - 4;
if(length == 0){
console.log("here comes");
if(this.props.router !== undefined) {
this.props.router.push('/');
} else {
console.log(this.props.router);
}
}
else{
console.log('work normally');
}
}
also you can visit their documentation to get more information from below url https://github.com/reactjs/react-router/blob/master/docs/guides/NavigatingOutsideOfComponents.md
You can get access to the history
prop from witRouter
higher order component, and use the push method on the history
object to push the route you want to.
import { withRouter } from "react-router";
class App extends React.Component {
componentWillMount() {
...
this.props.history.push('/');
...
}
}
export default withRouter(App);
Add two lines.
import { withRouter } from "react-router-dom";
this.props.history.push("/welcomePage");
本文标签: javascriptreactrouter quot Cannot read property 39push39 of undefinedquotStack Overflow
版权声明:本文标题:javascript - react-router " Cannot read property 'push' of undefined" - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738007200a2048281.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论