Why set state = props ?(为什么设置状态=道具?)
is an anti-pattern , you should keep using this.prop.list in the other component.(是一个反模式 ,您应该在其他组件中继续使用this.prop.list。)
Do this:(做这个:)
console.log(this.props.list)
it will print [], and then when the results come it will print the full array.(它将打印[],然后在结果到来时将打印整个数组。)
When you need to use this array always use this.props.list.(当您需要使用此数组时,请始终使用this.props.list。)
Based in your comment here is how you give solution to that: At the parent you add a function(根据您的评论,这里提供了解决方法:在父级中添加一个功能)
listUpdate(newList){
this.setState({
list: newList
})
}
and then you pass this function to the child(然后将此功能传递给孩子)
<LfMap list={this.state.dev} listUpdate={this.listUpdate}/>
when you need to update it you need to call this.props.listUpdate.(需要更新时,需要调用this.props.listUpdate。) Always update the props :) that's where the reference is, this is the pattern you should follow.(始终更新props :),这是参考所在,这是您应该遵循的模式。) 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…