reactjs - react-native 排序功能在 FlatList 中不起作用
问题描述
嗨,我正在使用 react-native 创建一个排序功能,
我正在使用Flatlist来显示列表项。当我在模拟器和网络中运行时,控制台中没有显示错误,但排序功能不起作用。
我的代码在下面请检查
import React, { Component } from 'react';
import contacts,{ compareNames } from './contacts'
import Card from './Card'
import {
View,
Button,
StyleSheet,
FlatList,
} from 'react-native';
const styles = StyleSheet.create({
mainwrap: {
display:'flex',
flexGrow: 1,
justifyContent: 'space-between',
paddingBottom:30,
},
txtheading:{
backgroundColor:'black',
margin:'auto',
marginTop:50,
},
scollitms:{
display:'flex',
flex:1,
position:'relative'
}
});
export default class App extends Component {
state = {
showCounter: true,
contacts:contacts
}
toggglecounter = () => {
this.setState((prev) => ({showCounter: !prev.showCounter}))
}
sorting = () => {
this.setState((prev) => ({
contacts: [...prev.contacts].sort(compareNames)
}))
}
renderItem = ({item}) => (<Card key={item.id} {...item} />)
render() {
return (
<View style={[styles.mainwrap]}>
<View style={[styles.marginfromtop , styles.countfont, styles.txtheading]}>
<Button style={{ marginTop: 50, zIndex:999}} onPress={this.toggglecounter} title="Toggle Contacts" />
<Button onPress={this.sorting} title="sort" />
</View>
{this.state.showCounter ?
<FlatList style={[styles.scollitms]}
data={this.state.contacts}
renderItem={this.renderItem}
keyExtractor={item => item.key.toString()}
/>
: null
}
</View>
);
}
}
下面是我正在调用的排序函数
export const compareNames = (contact1, contact2) => contact1.name > contact2.name
解决方案
推荐阅读
- java - 在使用按位 NOT 运算符时,Java 中如何将 5 表示为 -6 和 -5 表示为 4?
- ios - Swift:定位和调整自定义圆形进度视图 UIBezierPath
- c# - 使用 Crystal Report 显示来自 DataTable 源的特定字段的所有数据
- r - 每个 id 的重复数字序列,其中 id 的长度不相等
- amazon-web-services - 尝试从 boto3 获取快照 ARN
- python - 使用 MAP 或 TEXT 类型的 Cassandra 列
- python - 如果子字符串列表中的任何值包含在数据框中的任何列中,则过滤行
- google-sheets - 修改谷歌图纸尺寸
- python - 通过时如何获得正确的结果
在路线。烧瓶,Python,FlaskForm - java - 无法创建请求的服务 [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] Java 错误