我无法对存储在反应挂钩状态中的对象数组进行排序。此数组在排序后提供给平面列表。
const { notifications } = useContext(MainContext);
const [sortedNotifications, setSortedNotifications] = useState([]);
useEffect(() => {
setSortedNotifications(
[...notifications].concat([]).sort(function(x, y){
return new Date(y.created_at).getTime() - new Date(x.created_at).getTime();
})
)
}, [notifications]);
return <View style={{ flex: 1, backgroundColor: "white", }}>
{notifications !== null && notifications.length > 0 ? <FlatList
vertical
contentContainerStyle={{ paddingRight: 20, paddingLeft: 20, paddingBottom: 20, paddingTop: 20}}
showsVerticalScrollIndicator={false}
style = {styles.flatListStyle}
data = {sortedNotifications}
keyExtractor = {(item,i) => `key-${item.title}-${i}`}
renderItem = {({ item }) => {
return <TouchableOpacity activeOpacity={0.8} style={styles.viewStyle} onPress={() => props.navigation.navigate("Notification", { n: item })}>
<Text style={styles.textStyle}>{Capitalize(item.title)}</Text>
<Text style={styles.text2Style}>Received {moment.utc(item.created_at).fromNow()}</Text>
</TouchableOpacity>
}}></FlatList> : <View></View>}
</View>
物体的位置没有变化。任何帮助将不胜感激
拉丁的传说
函数式编程
慕村9548890
随时随地看视频慕课网APP
相关分类