javascript - Render Component every time it's get clicked
问题描述
I have a List of country that shows name and id. I want to able to edit the countries, so i add it edit button for each row. the structure of component is like :
ListComponent
-- Edit Component // will show a modal
--- Modal Component
---- Form Component
I should fill the form whenever modal is open, now the question is : which life cycle method should i use to fill the form ?
componentDidMount
only fire once.
componentDidUpdate
won't trigger on first call and will trigger afterward.
Code :
componentDidMount(prevProps, prevState) {
if(this.props.status == 'update') {
this.props.form.setFieldsValue({
isForeign: this.props.country.isForeign,
name: this.props.country.name,
});
}
}
解决方案
如果要在类组件中处理启动和更新,则必须同时使用这两种生命周期方法。这是 React 为功能组件实现钩子以避免冗余的一个重要原因。
推荐阅读
- c# - 实体框架:迁移每个表中的列名必须是唯一的
- javascript - 如何将 CodePen 中的笔嵌入到没有 CodePen 框架的网站中
- python - 有没有办法将整个 SBIR 奖项下载为 JSON 文件?
- android - 如何在双簧管 onAudioReady 方法中放大音频数据?
- android - 如何使片段适合所有屏幕尺寸的 FrameLayout?
- python - TensorFlow DNN 总是不适合我的数据集
- pandas - Groupby 在一列中的值是将 b 列的总和除以 A 列的总和的结果
- java - 将执行输出保存到
pom.xml 的部分,用于以后的 maven 阶段 - ansible - Ansible Unarchive 留下 *.gz 文件而不是目录
- javascript - 如果满足条件,更新收藏并发送电子邮件 || Mongo Nodejs