react-native - FlatList 参考问题
问题描述
我没有得到ref
总是flatlist
得到
警告:不能给函数组件提供参考。尝试访问此 ref 将失败。你的意思是使用
React.forwardRef()
?
我是 react-native 的初学者
export default class ListData extends Component {
constructor(props) {
super(props);
this.state = {
flatListRef: null
};
}
render() {
return (
<View style={styles.container}>
<FlatList
data={countryList}
ref={ref => (this.state.flatListRef = ref)}
style={{ flex: 1 }}
showsVerticalScrollIndicator={false}
showsHorizontalScrollIndicator={false}
renderItem={({ item, index }) => (
<View style={[styles.ListViewContainer]}>
<Text style={styles.countryTxt}>
<Text> +{item.code}</Text>
<Text> {item.CountryName} </Text>
</Text>
</View>
)}
keyExtractor={(item, index) => index.toString()}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: 40,
backgroundColor: "white"
}
});
解决方案
使用import { FlatList } from "react-native"
代替
import { FlatList } from 'react-native-gesture-handler';
正在工作中。但我仍然想知道为什么它不能与“react-native-gesture-handler”一起使用。虽然列表显示正确,但 ref 没有得到。