【问题标题】:undefined is not an object (evaluating 'this.setState') when Removing object from Flatlist从平面列表中删除对象时,未定义不是对象(评估“this.setState”)
【发布时间】:2021-07-12 10:33:02
【问题描述】:

所以我目前正在 React Native 中学习 Flatlists,我正在尝试编写一个函数来通过在 Button 中使用 onLongPress 来删除 Flatlist 中的项目,这是项目的示例,如它所示终端:

Array [
  Object {
    "Friday": false,
    "Monday": true,
    "Saturday": false,
    "Sunday": false,
    "Thursday": false,
    "Tuesday": false,
    "Wednesday": false,
    "key": 0.08631781113770953,
    "workoutName": "",
  },
]

这是我正在使用的功能,我按照教程获得:

removeItem(item){
    this.setState({
       workoutList: this.state.workoutList.filter((item)=>item.key !== item.key)
    });
}

这是我调用函数的地方:

openTwoButtonAlert = () => {
    Alert.alert(
       'Delete Workout',
      'Are you sure to delete this Workout?',
      [
        {text: 'Delete', onPress: this.removeItem(item)},

        {text: 'Cancel', 
           style: 'cancel',
           
        },
      ],
    );
  }

这是我的Flaltist:

 <FlatList
              data={this.state.workoutList}
              keyExtractor={(item, index) => item.toString()}
              renderItem={({ index, item}) => (
                <View style={{padding:20}}>
                  <TouchableOpacity onPress={() => this.props.navigation.navigate("WorkoutCreated", {
                    workoutName: item.workoutName,
                    Monday: item.Monday,
                    Tuesday: item.Tuesday,
                    Wednesday: item.Wednesday,
                    Thursday: item.Thursday,
                    Friday: item.Friday,
                    Saturday: item.Saturday,
                    Sunday: item.Sunday,
                  })} 
                  onLongPress={()=>this.openTwoButtonAlert(index)}>
                    <Text>{item.workoutName}</Text>       
                  </TouchableOpacity>
                </View>
              )}
            />

要更新的状态是锻炼列表,我正在使用类组件,当我启动代码时它返回错误:TypeError: undefined is not an object (evaluating 'this.setState') 提前感谢您的帮助。

【问题讨论】:

  • 分享你的完整代码。你在哪里打电话removeItem
  • 我编辑了上面的代码

标签: javascript reactjs react-native react-native-flatlist


【解决方案1】:

试试这个方法(更新)

<FlatList
   ...
    keyExtractor={(item, index) => item.toString()} // update here also
    renderItem={({item, index}) => ( // **** updated here ****
       .....
       onLongPress={() => this.openTwoButtonAlert(index)}
       ...
    )
   ... 
/>

openTwoButtonAlert = (index) => { // updated here
    Alert.alert(
      ...
        {text: 'Delete', onPress: () => this.removeItem(index)}, // change here
      ...
    );
}

removeItem(index){
    // updated code of removing item from list
    const newData = [...this.state.workoutList]; // this code is making clone of the data list
    newData.splice(index, 1); // remove item from list
    this.setState({workoutList: newData}); // reset data to list       
}

【讨论】:

  • @Salvo 你应该有item 你调用onPress 因为你期望item 作为removeItem 方法中的参数。
  • 可能错误是因为您没有在 ``ònLongPress`` 属性中传递 FlatListitem 参数。像这样在其参数中传递项目onLongPress={()=&gt;this.openTwoButtonAlert(data.item) }
  • @Salvo 我已经更新了我的代码,请检查
  • @Salvo 我已经更新了我的完整答案,请检查
  • @Salvo 更新您的keyExtractor 代码以消除警告
猜你喜欢
  • 2018-10-24
  • 1970-01-01
  • 2016-12-04
  • 2019-09-26
  • 2019-12-12
  • 2019-08-16
  • 2017-01-16
  • 2020-08-06
  • 2020-01-09
相关资源
最近更新 更多