[英]How to filter items in FlatList in react native
I am making an api call and storing the response in a state:我正在进行 api 调用并将响应存储在 state 中:
var api_response = resp.docs
setShift(api_response);
api response: api 响应:
"docs": [
{
"_id": "1",
"description": null,
"shift_date": "2021-12-03T18:30:00.000Z",
},
{
"_id": "2",
"description": null,
"shift_date": "2021-12-03T18:30:00.000Z",
},
{
"_id": "3",
"description": null,
"shift_date": "2021-11-03T18:30:00.000Z",
},
{
"_id": "4",
"description": null,
"shift_date": "2021-11-03T18:30:00.000Z",
},
],
Then I am using FlatList to show the contents on screen:然后我使用 FlatList 在屏幕上显示内容:
<FlatList
data={shift}
onRefresh={getShiftDetails}
refreshing={isLoading}
onEndReached={loadNextPage}
ListEmptyComponent={
<ErrorComponent
text={'List is empty!'}
style={{minHeight: 250}}
/>
}
renderItem={ItemView}
/>
Then in ItemView
function I wanted to filter out the data based on it's shift_date
, as in I want to show only those data which has a shift_date
of either today or date has to be in future.然后在
ItemView
function 中,我想根据它的shift_date
过滤掉数据,因为我只想显示那些shift_date
为今天或日期必须在未来的数据。 I don't want to show data which has shift_date
of past我不想显示过去
shift_date
的数据
const ItemView = ({item}) => {
//calculation to eliminate data which has `shift_date` in past::::
var todayy = moment().utcOffset(0, false);
var wantedData = item.filter(function (i: any) {
^^^
const element = i.shift_date;
var dateDiff = todayy.diff(element, 'days') * -1;
return dateDiff >= 0;
});
console.log('>>>>>>>>>>>>>>>>>>', item);
return (
<View key={item._id + '--'}>
<View>
<ShiftComponent
id={item}
dateOfShift={item.shift_date}
/>
</View>
</View>
);
};
I have consoled item
, it comes like:我安慰过
item
,它就像:
>>>>>>>>>>>>>>>>>> {"_id": "1","description": null,"shift_date": "2021-12-03T18:30:00.000Z",},
>>>>>>>>>>>>>>>>>> {"_id": "2", "description": null, "shift_date": "2021-12-03T18:30:00.000Z",},
>>>>>>>>>>>>>>>>>> {"_id": "3","description": null,"shift_date": "2021-11-03T18:30:00.000Z",},
>>>>>>>>>>>>>>>>>> {"_id": "4","description": null,"shift_date": "2021-11-03T18:30:00.000Z",},
I have written the calculation logic for finding data which does not have shift_date
of past.我已经编写了用于查找过去没有
shift_date
的数据的计算逻辑。 I know that my calculation is right, but I am getting error undefined is not a function
.我知道我的计算是正确的,但我得到错误
undefined is not a function
。 I have marked ^^^
where I am getting error.我已经标记了
^^^
我遇到错误的地方。 How can I solve my problem and show the data in renderItem
which does not include shift_date
of past.如何解决我的问题并在
renderItem
中显示不包括过去shift_date
的数据。
You should filter the data
array, not the item
.您应该过滤
data
数组,而不是item
。
Something like this像这样的东西
const currentTime = new Date().toISOString();
const filteredShifts = shift.docs.filter((x) => x.shift_date > currentTime);
const ItemView = ({item}) => {
return (
<View key={item._id + '--'}>
<View>
<ShiftComponent
id={item}
dateOfShift={item.shift_date}
/>
</View>
</View>
);
};
...
<FlatList
data={filteredShifts}
onRefresh={getShiftDetails}
refreshing={isLoading}
onEndReached={loadNextPage}
ListEmptyComponent={
<ErrorComponent
text={'List is empty!'}
style={{minHeight: 250}}
/>
}
renderItem={ItemView}
/>
...
If your data might change, make sure to use the extraData
prop on the FlatList.如果您的数据可能会更改,请确保使用
extraData
上的 extraData 属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.