Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native React Native:使用componentDidMount上的ref计算平面列表的scrollToItem并呈现它_React Native_React Native Flatlist - Fatal编程技术网

React native React Native:使用componentDidMount上的ref计算平面列表的scrollToItem并呈现它

React native React Native:使用componentDidMount上的ref计算平面列表的scrollToItem并呈现它,react-native,react-native-flatlist,React Native,React Native Flatlist,我有一个平面列表,它可以显示1-10之间的数字以及一个单选按钮。若我预先选择了可能是动态的数字,那个么编号单选按钮是预先选择的。在这种情况下,如何计算scrollToItem/scrollToIndex componentDidMount() { this.scrollToItem(); } scrollToItem = () => { let dynamicIndex = dynamicIndex //calcuate the dynamic Index

我有一个平面列表,它可以显示1-10之间的数字以及一个单选按钮。若我预先选择了可能是动态的数字,那个么编号单选按钮是预先选择的。在这种情况下,如何计算scrollToItem/scrollToIndex

componentDidMount() {
     this.scrollToItem();
}

 scrollToItem = () => {
    let dynamicIndex = dynamicIndex 
  //calcuate the dynamic Index

   this.flatListRef.scrollToIndex({ animated: true, index: dynamicIndex });

};

 <FlatList
    ref={ref => {this.flatListRef = ref;}}
    bounces={false}
    keyExtractor={item => item.id}
    data={listData}
    renderItem={({ item, index })=>
       this.renderItem(item,index)}
   />
componentDidMount(){
this.scrollToItem();
}
scrollToItem=()=>{
设dynamicIndex=dynamicIndex
//计算动态指数
this.flatListRef.scrollToIndex({animated:true,index:dynamicIndex});
};
{this.flatListRef=ref;}}
反弹={false}
keyExtractor={item=>item.id}
数据={listData}
renderItem={({item,index})=>
this.renderItem(项,索引)}
/>

但这会带来错误无法读取索引的属性“scrollToIndex”。基本上,它在组件安装期间找不到'this.flatListRef'

它看起来不像您在任何地方定义的
flatListRef
。只需添加
flatListRef:any
componentDidMount
方法上方。它看起来不像您在任何地方定义的
flatListRef
。只需添加
flatListRef:any
componentDidMount
方法上方。