【发布时间】:2019-05-04 05:12:34
【问题描述】:
我没有得到ref 的flatlist 总是得到
警告:函数组件不能被赋予 refs。尝试访问 这个裁判会失败。你的意思是使用
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"
}
});
【问题讨论】:
-
不要
this.state.flatListRef,只要this.flatListRef -
@Vivek_Neel 不走运,仍然出现同样的错误。它对你有用吗?
标签: react-native react-native-flatlist