我有两个动作。有时,该值返回未定义,因此我没有收到所有用户的帖子,而是收到了用户的帖子。我需要找到一种更好的方法来使我的功能一起工作。
现在我有:
componentDidMount(){
this.props.fetchUser();
let userId = this.props.user.map((user) => (user.id));
console.log(userId)
this.props.fetchPostsFromUser(userId);
}
因为它无法立即获取userId,所以不会在用户搜索的末尾添加参数,因此它会给我所有帖子。
[如果我尝试做其他类似的事情
componentDidMount(){
this.props.fetchUser();
}
componentWillReceiveProps(){
let userId = this.props.user.map((user) => (user.id));
console.log(userId)
this.props.fetchPostsFromUser(userId);
}
它会循环播放,但会在控制台中无限循环。
也将下面的代码放入渲染器中也将使其永远循环。
let userId = this.props.user.map((user) => (user.id));
console.log(userId)
this.props.fetchPostsFromUser(userId);
进行此操作的最佳方法是什么,以便它按我希望的方式工作。