React组件未在我的项目中运行componentDidmount,因此我收到错误isEdit的未定义?

时间:2020-08-15 01:49:12

标签: reactjs react-native

当我运行react-native组件时,出现以下错误:

但是,在constructorcomponentWillmount的调试器中以及渲染生命周期中,isEditfalse

componentDidmount不打印任何内容,因此我认为该组件未运行componentDidmount生命周期,并且我不知道发生此错误的原因。

constructor(props: IProps) {
    // isEdit: false

    super(props);
    // const {navigation} = this.props;
    props.navigation.setOptions({
      headerRight: () => {
        return <HeaderRightBtn onSubmit={this.onSubmit} />;
      },
    });
  }
componentWillmount() {
    // isEdit: false
}
componentDidmount() {
    // isEdit of undefined
}
render() {
    // isEdit: false
    const {categorys, isEdit} = this.props;
    const {myCategorys} = this.state;
    const classifyGroup = _.groupBy(categorys, (item) => item.classify);
    return (
      <ScrollView style={styles.container}>
        <Text style={styles.classifyName}>我的分类</Text>
        <View style={styles.classifyView}>
          <DragSortableView
            dataSource={myCategorys}
            renderItem={this.renderItem}
            sortable={isEdit}
            fixedItems={fixedItems}
            keyExtractor={(item) => item.id}
            onDataChange={this.onDataChange}
            parentWidth={parentWidth}
            childrenWidth={itemWidth}
            childrenHeight={itemHeight}
            marginChildrenTop={margin}
            onClickItem={this.onClick}
          />
          {/* {myCategorys.map(this.renderItem)} */}
        </View>
        <View>
          {Object.keys(classifyGroup).map((classify) => {
            return (
              <View key={classify}>
                <Text style={styles.classifyName}>{classify}</Text>
                <View style={styles.classifyView}>
                  {classifyGroup[classify].map((item, index) => {
                    if (
                      myCategorys.find(
                        (selectedItem) => selectedItem.id === item.id,
                      )
                    ) {
                      return null;
                    }
                    return this.renderUnSelectedItem(item, index);
                  })}
                </View>
              </View>
            );
          })}
        </View>
      </ScrollView>
    );
  }
}

1 个答案:

答案 0 :(得分:0)

正确的方法名称是 分别根据componentdidmountunsafe_componentwillmount的“ componentDidMount()”和“ UNSAFE_componentWillMount()”。

请注意,根据official docs,componentWillMount()更改为UNSAFE_componentWillMount()。