首页 > 解决方案 > 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"
  }
});

标签: react-nativereact-native-flatlist

解决方案


使用import { FlatList } from "react-native" 代替 import { FlatList } from 'react-native-gesture-handler';

正在工作中。但我仍然想知道为什么它不能与“react-native-gesture-handler”一起使用。虽然列表显示正确,但 ref 没有得到。


推荐阅读