【发布时间】:2021-08-06 14:48:58
【问题描述】:
无法从 flatList 中删除项目在来自 Api 的 react-native 中,ApiCard 是另一个组件,其中所需的数据项有一个 del 按钮..
import React, {Component} from 'react'; import { StyleSheet, View, FlatList, Text, TouchableOpacity, SafeAreaView, Button, } from 'react-native'; import ApiCard from './ApiCard';
export default class ApiList extends Component {
constructor(props) {
super(props);
this.state = {
loading: true,
dataSource: [],
};
}
static navigationOptions = {
title: 'List of countries',
};
componentDidMount() {
fetch('https://restcountries.eu/rest/v2/all')
.then(response => response.json())
.then(responseJson => {
this.setState({
loading: false,
dataSource: responseJson,
});
})
.catch(error => console.log(error));
}
render() {
const {dataSource} = this.state;
const {navigation} = this.props;
return (
<SafeAreaView style={styles.container}>
<FlatList
data={dataSource}
keyExtractor={item => item.name}
renderItem={data => (
<ApiCard {...data.item} navigation={navigation} />
)}
/>
</SafeAreaView>
);
}
}
Below 组件用于将数据渲染为道具
import React from 'react';
import {TouchableOpacity, Text, StyleSheet, Button} from 'react-native';
const ApiCard = ({name, navigation, alpha2Code, population, capital}) => {
return (
<TouchableOpacity
style={styles.country}
onPress={() =>
navigation.push('CountryName', {
name,
alpha2Code,
population,
capital,
})
}>
<Text>{name}</Text>
<Text>{alpha2Code}</Text>
<Button title="del" /
这里我需要一个 del 按钮操作 它没有删除任何东西并且删除操作不起作用
请帮我找到解决办法 我尝试了所有方法,但我无法删除它
</TouchableOpacity>
);
};
export default ApiCard;
【问题讨论】:
标签: reactjs react-native