[英]How to setstate with arrays in React Native
I am getting an from External APi, by which i am setting State in ComponentWillMount, but it only stores the last value of array, not the whole list of array. 我从外部APi获取一个,通过它在ComponentWillMount中设置State,但是它仅存储数组的最后一个值,而不存储数组的整个列表。 What am i doing wrong ?
我究竟做错了什么 ?
Here is my code: Update, My Full Component Code 这是我的代码: 更新,我的完整组件代码
class PlanStatus extends Component { PlanStatus类扩展Component {
componentWillMount() {
const { phone, orderNo } = this.props.navigation.state.params;
this.props.fetchPlanStatus({phone, orderNo}, (snapShotValue) => {
const fetchedPlans = snapShotValue;
//_.map(snapShotValue.orderValidity, (val,uid) => ({uid, ...val}))
const markedDates = fetchedPlans.orderValidity[0].deliveryDates.forEach(element => {
this.setState((prevState) => ({ dates: [...prevState.dates, element.date], deliveryStatus: element.deliveryStatus, startingDay: [element.startingDay], endingDay: element.endingDay }));
console.log(element.date)
});
this.setState({ finalPlanStatus: fetchedPlans })
});
}
onPressDateHandler(day) {
this.setState({ date: day });
console.log(this.state.dates);
}
state = {date: "", finalPlanStatus: null, dates: [], deliveryStatus: [],
startingDay: "", endingDay: []}
render () {
const date = format(new Date(), 'YYYY-MM-DD');
return (
<Container>
<View style={{ paddingTop: 30, paddingLeft: 40 }}>
<H1>Plan Status</H1>
</View>
{this.props.spinner && <Spinner color='blue' />}
<View style={this.props.spinner === false ? { display:'flex', paddingTop: 20 } : { display:'none', paddingTop: 20 }}>
<Calendar
minDate={date}
maxDate={'2019-02-20'}
onDayPress={(day) => this.onPressDateHandler(day)}
markedDates={
{'2019-02-02': {color:'#22a6b3', startingDay: true, selected: true},
'2019-02-03': {color: '#22a6b3'},
'2019-02-05': {endingDay: true, color: '#22a6b3', textColor: 'gray'},
'2019-02-04': {color: '#22a6b3', selected: true}
}}
displayLoadingIndicator
markingType={'period'}
/>
</View>
<View>
<Text>{this.state.date.dateString}</Text>
</View>
</Container>
);
}
} }
By this, while doing forEach on deliveryDates, it correctly shows the list of array as follows: 这样,在deliveryDates上执行forEach时,它可以正确显示数组列表,如下所示:
deliveryDates: Array(3)
0: {date: "2019-02-03", deliveryStatus: false, endingDay: false, startingDay: true}
1: {date: "2019-02-04", deliveryStatus: false, endingDay: false, startingDay: true}
2: {date: "2019-02-05", deliveryStatus: false, endingDay: false, startingDay: false}
length: 3
__proto__: Array(0)
And, then from this, i am setting the states as above code. 然后,从这开始,我将状态设置为上述代码。 But, my state value is only being the last value of the array.
但是,我的状态值只是数组的最后一个值。 And here is my initial state,
这是我的初始状态
state = {finalPlanStatus: null, dates: [], deliveryStatus: [], startingDay: "", endingDay: []}
Try this, 尝试这个,
componentWillMount() {
const { phone, orderNo } = this.props.navigation.state.params;
this.props.fetchPlanStatus({phone, orderNo}, (snapShotValue) => {
const fetchedPlans = snapShotValue;
const dates = fetchedPlans.orderValidity[0].deliveryDates.map(element => element.date)
this.setState((prevState) => { return { dates: [...prevState.dates, ...dates], finalPlanStatus: fetchedPlans } })
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.