reactjs - 未调用 React-Native onScroll 事件
问题描述
我正在尝试创建一个分页/无限滚动组件,但该onScroll
事件从未被触发。
有谁知道我在这里做错了什么?
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { ScrollView } from 'react-native';
class PaginatedScrollView extends Component {
static propTypes = {
paginationSize: PropTypes.number,
data: PropTypes.array.isRequired,
renderItem: PropTypes.func.isRequired
}
handleOnScroll (event) {
console.log(event.nativeEvent.contentOffset.y);
}
render () {
const {
data,
renderItem,
paginationSize
} = this.props;
return (
<ScrollView
onScroll={e => this.handleOnScroll(e)}
onContentSizeChange={console.log}
>
{data
.slice(1, paginationSize)
.map(renderItem)
}
</ScrollView>
);
}
}
export default PaginatedScrollView;
似乎onContentSizeChange
被调用但onScroll
没有。
解决方案
您需要在构造函数中绑定handleOnScroll函数或将其更改为箭头函数
在构造函数中绑定handleOnScroll
constructor(props){
super(props);
this.handleOnScroll = this.handleOnScroll.bind(this);
}
或者
改成箭头函数
handleOnScroll = event => {
console.log(event.nativeEvent.contentOffset.y);
}
推荐阅读
- c# - 如何将按钮从子窗体动态添加到主窗体
- sql - 如果 col b 的值匹配,则用 col a 的值填充 col a 的 NULL
- python - OpenCV:如何计算灰度的平均值并将输出保存到 txt 文件
- android - 将两个 TextView 放在一个聊天气泡中
- javascript - 延迟屏蔽输入字段的时间
- php - 获取对按键搜索的php数组元素的引用
- android - 用于移动应用程序的 WooCommerce 购物车 API
- sql - 如何编写 SQL 查询来确定登录次数最多的用户的用户名和电子邮件地址
- sql - SQL - 使用查询字符串删除 url 中的尾随“/”
- java - 使用 apache lang DateUtil 的日期格式失败