从平面列表中删除对象时出现问题

uidvcgyl  于 2021-09-23  发布在  Java
关注(0)|答案(0)|浏览(161)

所以我尝试从平面列表中添加和删除对象,我使用react导航和 getDerivedStateFromProps ,在添加对象时一切正常,但在删除对象时返回错误: undefined is not an object (evaluating 'List.filter') ,我用 onLongPress 发起 Alert 并通过警报确认用户是否要删除该对象。代码如下:

export class ListPage extends Component {
  constructor(props) {
    super(props);
    this.state = {
      List: [],
    };
  }

  static getDerivedStateFromProps(props, state) {
    if (props.route.params?.list) {
      return {
        List: [...state.List, props.route.params.list],
      };
    }
    return null;
  }

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

        {text: 'Cancel', 
           style: 'cancel',

        },
      ],
    );
  }

  removeItem = (List) => {
    let filteredItems = List.filter(item => item.key !== key);
    this.setState({ List: filteredItems })
  }

这是一个平面列表:

<FlatList
          data={this.state.List}
          keyExtractor={(item, index) => item.key.toString()}
          renderItem={(data) => (
            <TouchableOpacity
               onLongPress={this.openTwoButtonAlert}
             />
        />

这就是终端显示的内容:

Array [
  Object {
    "Friday": false,
    "Monday": true,
    "Saturday": false,
    "Sunday": false,
    "Thursday": 0,
    "Tuesday": false,
    "Wednesday": false,
    "key": 0.6245429699919928,
    "Name": "",
  },
]

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题