【发布时间】:2019-12-12 15:11:21
【问题描述】:
从 ListView 迁移到 Flatlist 时,我在一个对象中获取值,我需要将其作为数组中的值获取。
我已将 react-native-modal-filter-picker 修改为需要为我工作的自定义组件。这里 onSelect 显示对象中的结果。
renderOptionList = () => {
const {
noResultsText,
listViewProps,
keyboardShouldPersistTaps,
keyExtractor,
} = this.props
const { ds } = this.state;
if (!ds.length) {
return (
<FlatList
data={ds}
keyExtractor={keyExtractor||this.keyExtractor}
{...listViewProps}
renderItem={() => (
<View style={styles.noResults}>
<Text style={styles.noResultsText}>{noResultsText}</Text>
</View>
)}
/>
)
} else {
return (
<FlatList
keyExtractor={keyExtractor||this.keyExtractor}
{...listViewProps}
data={ds}
renderItem={this.renderOption}
/>
)
}
};
renderOption = ({item}) => {
const {
selectedOption,
renderOption,
optionTextStyle,
selectedOptionTextStyle
} = this.props;
const { key, label } = item
let style = styles.optionStyle;
let textStyle = optionTextStyle||styles.optionTextStyle;
if (key === selectedOption) {
style = styles.selectedOptionStyle;
textStyle = selectedOptionTextStyle ||styles.selectedOptionTextStyle
}
if (renderOption) {
return renderOption(item, key === selectedOption)
} else {
return (
<TouchableOpacity activeOpacity={0.7}
style={style}
onPress={() => this.props.onSelect(item)}
>
<Text style={textStyle}>{label}</Text>
</TouchableOpacity>
)
}
};
【问题讨论】:
标签: react-native react-native-flatlist