【发布时间】:2019-07-22 04:20:32
【问题描述】:
我正在使用“react-native-swipe-list-view”来尝试删除项目。出于某种原因,当我尝试删除一个项目时,它正在从平面列表中删除最后一个项目,即使该项目仍显示在数组中并且正确的项目已被删除。
当我删除一个项目时,基本上总是从平面列表中删除最后一个项目,即使该项目仍然存在于数据数组中。很奇怪的活动。当我离开并返回页面时,会显示正确的数据。
CoinList.js
import React, { Component } from "react";
import { FlatList, StyleSheet, Text, View , AsyncStorage } from "react-native";
import CoinListRow from './CoinListRow';
class CoinList extends Component {
constructor(props) {
super(props);
this.state = {
coinList: null
};
}
updateCoinList = (updatedCoins) => {
this.setState(
{coinList: updatedCoins}
)
}
componentDidMount() {
AsyncStorage.getItem("coins").then(value => {
// need to convert returned object into an array
this.setState({coinList: Object.values(JSON.parse(value))})
});
}
render() {
return (
<View style={styles.container}>
{this.state.coinList !== null ? <FlatList
data={this.state.coinList}
keyExtractor={(item, index) => index.toString()}
extraData={this.state}
renderItem={({item}) => <CoinListRow style={styles.item} item={item} updateCoinList={this.updateCoinList}/>}
/> : null}
</View>
);
}
}
export default CoinList;
CoinListRow:
import React, { Component } from "react";
import { withNavigation } from "react-navigation";
import { AsyncStorage } from "react-native";
import { View, StyleSheet, Text, TouchableOpacity } from "react-native";
import { ListItem } from "react-native-elements";
import { SwipeRow } from "react-native-swipe-list-view";
import images from "../../assets/logos/coinLogos";
class CoinListRow extends Component {
constructor() {
super();
this.state = {
coinInfo: null,
disableInterstitialBtn: false
};
}
componentDidMount() {
const { item } = this.props;
return fetch(`https://api.coincap.io/v2/assets/${item.card.slug}`)
.then(response => response.json())
.then(responseJson => {
console.log(responseJson);
this.setState({
isLoading: false,
coinInfo: responseJson.data
});
})
.catch(error => {
console.error(error);
});
}
removeCoin = () => {
AsyncStorage.getItem('coins')
.then((coins) => {
AsyncStorage.removeItem('coins');
let c = coins ? JSON.parse(coins) : {};
delete c[this.props.item.card.slug]
AsyncStorage.setItem('coins', JSON.stringify(c));
this.props.updateCoinList(Object.values(c));
})
.catch((error)=> {
console.log(error);
alert(error);
}
)
}
render() {
const { coinInfo } = this.state;
const { item } = this.props;
console.log(this.state.coinInfo);
return (
<View>
{this.state.coinInfo ? (
<SwipeRow disableRightSwipe={true} rightOpenValue={-120}>
<View style={styles.standaloneRowBack}>
<Text style={styles.backTextWhite}></Text>
<TouchableOpacity onPress={this.removeCoin}><Text style={styles.backTextWhite}>Remove Item</Text></TouchableOpacity>
</View>
<View>
<ListItem
key={item.card.slug}
leftAvatar={{ source: images[item.card.slug] }}
title={coinInfo.name}
titleStyle={{ fontWeight: "bold" }}
subtitle={coinInfo.symbol}
onPress={this._openInterstitial}
chevron
bottomDivider={true}
/>
</View>
</SwipeRow>
) : null}
</View>
);
}
}
export default withNavigation(CoinListRow);
【问题讨论】:
标签: react-native asynchronous react-native-flatlist asyncstorage