首页 > 解决方案 > 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,
        });
      }
  }

标签: javascriptreactjsredux

解决方案


如果要在类组件中处理启动和更新,则必须同时使用这两种生命周期方法。这是 React 为功能组件实现钩子以避免冗余的一个重要原因。

https://reactjs.org/docs/hooks-intro.html


推荐阅读