- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图使用 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)}
/>
最佳答案
要访问当前渲染的 ref,您需要使用 .current
- 所以在你的情况下,使用 refContainer.current
:
useEffect(()=>{
if(refContainer.current){
refContainer.current.scrollToIndex({ animated: true, index: 0 });
}
},[status])
<FlatList ref={refContainer} ...
.current
的更多信息,请参阅
docs)
关于reactjs - Flatlist scrollToIndex with Hooks useRef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60566164/
我目前有一个 map 和一个旋转木马(类似于FlatList),我希望能够使用FlatList 的scrollToIndex 方法捕捉到阵列中的某个项目。问题是我何时采用了 react-native-
我有一个 FlatList,我试图每隔 X 秒滚动一次数据数组的每个索引。现在我的数组中只有两个项目,但可能还有更多。当前代码适用于前两次迭代,但似乎没有正确重置,我得到 scrollToIndex
我正在开发一个聊天应用程序,当列表加载时,当一个新项目添加到列表时,我需要滚动到列表底部。我可以用这个来做。 scrollToBottom() { let lv = frame.topmost(
(此问题与 react-virtualized 库有关) 我有一个使用 的组件显示几类项目。结果输出类似于: Jump to: Planets, Nebulae PLANETS - Mercury
我试图使用 React Hooks 和方法 scrollToIndex 滚动到我的数据在 flatlist 的中间,但我无法引用它。我使用类似 ref={component => (this.list
尝试 scrollToIndex 时,屏幕变为空白。 这是一个重现问题的代码和框。只需在顶部的输入框中输入 100,然后单击“滚动到行”按钮。 https://codesandbox.io/s/zr2
我正在使用 flatList要从 json 文件呈现项目,我想在按下按钮时滚动到特定索引,我声明了按下按钮的功能,如下所示 goIndex = () => { this.flatListRef
我是 React Native 的新手,我遇到了一个我自己无法解决的问题。 到目前为止,这是我使用它的方式: export default class Form extends React.Compo
我正在使用新的 FlatList component并想利用 ScrollToIndex (或 ScrollToEnd )在生命周期方法中,例如 componentDidMount . 我有一个比方说
我在 iOS 上成功使用了 scrollToIndex,但同样的代码不适用于 android。有什么想法吗? this.list.scrollToIndex({animated: true, inde
我的 React Native 应用程序中有这个类别过滤器组件。第一次加载此组件时,它不会滚动到给定的索引项(即 3)。我查了一下,功能scrollToIndex正在调用。但是当组件重新渲染时加载屏幕
我正在使用顶部带有动画 FlatList 的 React Native map ,当我按下 map 标记时,我希望列表滚动到该特定索引。但是当我尝试这样做时,我得到了这个错误 _this.flatLi
setTimeout(() => { this.myFlatList.scrollToIndex({animated:true , index: 100}) }, 100); 如果我在平面列表中使用
也许这看起来很容易,但我真的不知道如何解决。 我的 NativeScript-vue 应用程序中有一个 ListView 组件。在我点击按钮后,我需要 ListView 向下滚动到它的末尾。 我试过这
我正在使用 react-native-swiper-flatlist ,我想通过点击按钮向前滚动到某个索引,但不知道该怎么做。虽然我是 React Native 开发的新手。 我已经搜索了很多但没有任
我是一名优秀的程序员,十分优秀!