简体   繁体   English

React Redux-在动作道具上未定义的无限滚动调用

[英]React Redux - Infinite Scroll call undefined on action props

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { actions, getBrands } from '../reducer';
import Infinite from 'react-infinite';
import SearchBox from '../components/SearchBox';
import CardList from '../components/CardList';

const { fetchBrands } = actions;

class BrandList extends Component {
    componentDidMount() {
        this.props.fetchBrands({ page: 1 });
    }

    renderList() {
        const brands = this.props.brands;
        return brands.map((brand) => {
            return (
                <CardList key={brand.id} name={brand.name} avatar={brand.avatar.thumbnail} follower={brand.follows_count} />
            );
        });

    }

    toggle() {
        return this.props.isFetching;
    }

    loadMore() {
        this.props.fetchBrands({ page: 2 });
    }

    render() {
        return (
            <div>
                <SearchBox />

                <div className="row">
                    <Infinite
                        elementHeight={145}
                        onInfiniteLoad={this.loadMore}
                        infiniteLoadBeginBottomOffset={200}
                        isInfiniteLoading={this.toggle()}
                        useWindowAsScrollContainer
                        >
                        {this.renderList()}
                    </Infinite>
                </div>
            </div>
        );
    }
}

function mapStateToProps(state) {
    return {
        brands: getBrands(state),
        isFetching: state.brand.isFetching
    };
}

export default connect(mapStateToProps, { fetchBrands })(BrandList);

The problem is when the page is load it always return me Uncaught TypeError: Cannot read property 'fetchBrands' of undefined . 问题是页面加载时总是返回我Uncaught TypeError: Cannot read property 'fetchBrands' of undefined But if I remove loadMore() function it successfully render without error. 但是,如果我删除loadMore()函数,它将成功呈现而没有错误。

What's the solution? 有什么解决方案?

may be scope problem. 可能是范围问题。 call loadMore with proper scope or use autobind decorator ( https://www.npmjs.com/package/core-decorators ) 在适当范围内调用loadMore或使用autobind装饰器( https://www.npmjs.com/package/core-decorators

 <Infinite
        elementHeight={145}
        onInfiniteLoad={() => this.loadMore()}
        infiniteLoadBeginBottomOffset={200}
        isInfiniteLoading={this.toggle()}
        useWindowAsScrollContainer >
        {this.renderList()}
 </Infinite>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM