0
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. But if I remove loadMore() function it successfully render without error.

What's the solution?

2
  • 1
    you forgot to bind load more function. onInfiniteLoad={this.loadMore.bind(this)} Commented Dec 7, 2016 at 3:37
  • @duwalanise thanks! it worked now. Commented Dec 7, 2016 at 3:43

1 Answer 1

1

may be scope problem. call loadMore with proper scope or use autobind decorator (https://www.npmjs.com/package/core-decorators)

 <Infinite
        elementHeight={145}
        onInfiniteLoad={() => this.loadMore()}
        infiniteLoadBeginBottomOffset={200}
        isInfiniteLoading={this.toggle()}
        useWindowAsScrollContainer >
        {this.renderList()}
 </Infinite>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.