react-native - 如何从定制的 flatlist 组件访问 flatList 方法,例如 scrollToIndex?
问题描述
我List
从 react-native flatList 组件创建了一个自定义组件。现在我想scrollToIndex
使用 ref 使用 flatlist 组件的方法,以便我可以通过按自定义按钮滚动到列表顶部。但是当我创建自定义组件时,scrollToIndex 方法似乎不可用。如何使这项工作?
解决方案
你可以通过道具传递下去
您的自定义组件“列表”文件:
const List = (props) => {
return (
<Flatlist
ref={(ref) => { this.listRef = ref; }}
initialScrollIndex={this.props.initialScrollIndex}
/>
);
};
正在使用:
scrollToIndex = () => {
this.listRef.scrollToIndex({animated: true, index: whateverIndex});
}
<List
initialScrollIndex={whateverIndex}
/>
推荐阅读
- reactjs - 如何在页面之间导航以使用钩子做出反应
- rust - `cargo` 使用 `x86_64-w64-mingw32-ld`,即使指定了 `linker=lld-link`
- python - 在 python 中安装 VCS 模块时出现问题:命令错误
- numpy - 在 Elipse 中,python 共享对象文件难以辨认
- java - Spring 5.2.2 版本无法连接自定义消息转换器
- python-3.x - 如何让我的熊猫脚本导出到 csv
- javascript - Javascript 框架无法正常工作与 jsfiddle 相同的代码?
- python - 一旦我训练了用于文本分类的 keras 模型,有什么方法可以知道特征的重要性?
- huawei-mobile-services - 在华为设备上运行我的游戏时收到错误返回码 7005
- p5.js - 在 for/in 语句中执行对象数组元素的方法时出错。p5.js