Tia*_*mas 5 reactjs react-native react-hooks
我试图使用 React Hooks 和方法 scrollToIndex 滚动到我在 flatlist 中的数据的中间,但我无法引用它。我使用类似 ref={component => (this.list = component)} 的类实现了这一点,但我可以通过 useRef 实现。
const refContainer = useRef(null);
useEffect(()=>{
if(refContainer){
refContainer.scrollToIndex({ animated: true, index: 0 });
}
},[status])
<FlatList
ref={()=>refContainer}
refreshing={loading}
onRefresh={() => console.log('refreshing')}
keyExtractor={(item, index) => item.date}
showsVerticalScrollIndicator={false}
style={{flex: 1,}}
data={kits}
onEndThreshold={0}
renderItem={({item, index}) => renderItem(item, index)}
/>
Run Code Online (Sandbox Code Playgroud)
向我显示错误:refContainer.scrollToINdex 不是函数。
Wil*_*ins 10
要访问当前渲染的 ref,您需要使用.current
- 所以在您的情况下,请使用refContainer.current
:
useEffect(()=>{
if(refContainer.current){
refContainer.current.scrollToIndex({ animated: true, index: 0 });
}
},[status])
Run Code Online (Sandbox Code Playgroud)
另外,像这样设置你的参考:
<FlatList ref={refContainer} ...
Run Code Online (Sandbox Code Playgroud)
(有关更多信息,请参阅文档.current
)
归档时间: |
|
查看次数: |
6998 次 |
最近记录: |