reactjs - React-Native onScroll 事件没有被调用

标签 reactjs react-native scrollview

我正在尝试创建分页/无限滚动组件,但 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 没有被调用。

最佳答案

您需要在构造函数中绑定(bind)handleOnScroll函数或将其更改为箭头函数

在构造函数中绑定(bind)handleOnScroll

  constructor(props){
       super(props);
       this.handleOnScroll = this.handleOnScroll.bind(this);
  }

或者

改为箭头函数

 handleOnScroll = event => {
    console.log(event.nativeEvent.contentOffset.y);
}

关于reactjs - React-Native onScroll 事件没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52882326/

相关文章:

reactjs - ES6 解构对象中的三元条件

android - React-native 应用程序中的业务逻辑在哪里?

react-native - 与关于 'ScrollView' 的 React Native 文档混淆

Android:如何检查 ScrollView 内的 View 是否可见?

android - 在 Android 上动态添加 TextView 到 ScrollView

javascript - 使用reactstrap导航栏进行 react : how to add and remove class on navbar tag when window scrolls

javascript - 如何在JSX(React)中执行JS而不将其输出为HTML?

react-native - Axios拦截器-在解决AsyncStorage之前不使用实例吗?

javascript - 从 apisauce 中的 SecureStore 加载 header

node.js - 分页/无限滚动时 Firebase endAt() 与 orderByChild() 的时髦行为