我对react和react-router非常陌生。
react的问题是,在我的应用程序react-router的一些页面上,react-router正在工作,一些页面给出了这样的错误:“无法读取未定义的属性'push‘”。
我使用的是react 0.14.1
我的路由代码如下所示:
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'));我的组件现在是这样的:
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');
}
}
}有人能帮我吗?谢谢。
发布于 2020-05-10 13:59:23
添加两行。
import { withRouter } from "react-router-dom";
this.props.history.push("/welcomePage");https://stackoverflow.com/questions/39184049
复制相似问题