React native React Native:使用componentDidMount上的ref计算平面列表的scrollToItem并呈现它
我有一个平面列表,它可以显示1-10之间的数字以及一个单选按钮。若我预先选择了可能是动态的数字,那个么编号单选按钮是预先选择的。在这种情况下,如何计算scrollToItem/scrollToIndexReact 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
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
方法上方。