據我所知,你的組件設置如下:
class Parent .... {
....
fetchUser(){
....
}
....
render(){
return (
...
<BaseMap fetchUser={this.fetchUser.bind(this)}/>
...
);
}
}
class BaseMap .... {
....
render(){
let user = this.props.fetchUser();
return (
<div> { ...use user here ... }</div>
);
}
}
請糾正我,如果我錯了。
您被建議做的是將調用fetchUser從BaseMap組件的render方法中移出並移至其componentDidMount方法。然後將用戶存儲在組件的狀態中並從那裏使用它。
class BaseMap .... {
constructor(props){
super(props);
this.state={user:null};
}
componentDidMount(){
let user = this.props.fetchUser();
this.setState({user:user});
}
render(){
return (
<div> {this.state.user?
...use user here ...
: "loading data"
}</div>
);
}
}
你在哪裏調用'fetchUser'?你可以發佈' '的代碼嗎?聽起來像他們的意思是' '你應該在'componentDidMount'中調用'this.props.fetchUser'? –