我有以下反应代码,其中调用处理程序(因为在render()函数中呈现了一个Route)在componentDidMount之后。我原本认为在渲染发生后和动作方法之后会调用componentDidMount。
handler() {
console.log('RouteCls:handler setting state true');
this.setState({
routeNotFound: true
});
}
componentDidMount() {
console.log('RoutesCls:cdm:' + this.state.routeNotFound);
this.props.action(this.state.routeNotFound);
}
render() {
return (
<div>
<Switch>
<Route exact path="/" component={Home}/>
<Route exact path="/speakers" component={Speakers}/>
<Route exact path="/login" component={Login}/>
<Route render={props => <RouteNotFound action={this.handler} />}></Route>
</Switch>
<div> in RoutesCls: { JSON.stringify(this.state, null, 2) }</div>
</div>
);
}