【发布时间】:2019-06-01 07:26:59
【问题描述】:
我有三个组件; Parent,ChildA,Child B。父级定义如下:
class Parents extends Component {
getPersons = () => {
fetch('/Persons')
.then(persons => this.setState({ persons:persons })
}
updateTable = () =>
this.setState({ update: true })
}
现在,ChildA 是一个表单,我可以在其中创建新的persons。 ChildB 是一张显示所有人的表格。当我创建一个新人时,我希望 ChildB 重新渲染。
我尝试在 getPerons() 之后立即在 childA 上调用 updateTable() 函数,如下所示:
class ChildA extends Component {
onSubmit() {
this.getPersons();
this.updateTable();
}
}
问题是表格在获取人员之前已更新。 处理这个问题的正确方法是什么?
我想可以在 childA 中定义 getPersons(),然后添加“.then(this.updateTable())”,但我也在初始渲染中使用 getPersons,所以我不想将它移开家长
【问题讨论】:
-
什么是
this.getTeachers();? -
@DarrenSweeney - 抱歉,现在编辑到 this.getPersons(); - 我的错误
标签: javascript reactjs