React无限滚动永远地提出请求

时间:2018-05-25 08:05:47

标签: javascript reactjs infinite-scroll

当滚动到达结尾时,我正在使用此库https://www.npmjs.com/package/react-infinite-scroller来加载更多内容。

我的代码如下所示:

import React from 'react';
import {Route, Link} from 'react-router-dom';
import FourthView from '../fourthview/fourthview.component';
import {Bootstrap, Grid, Row, Col, Button, Image, Modal, Popover} from 'react-bootstrap';
import traineeship from './traineeship.api';
import Header from '../header/header.component';
import InfiniteScroll from 'react-infinite-scroller';

require('./traineeship.style.scss');

class Traineeship extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            companies: [],
            page: 0,
            resetResult: false,
        };
    }

    componentDidMount() {
        this.fetchCompanies(this.state.page);
    }

    fetchCompanies = page => {
        traineeship.getAll(page).then(response => {
            if (response.data) {
                const companies = Array.from(this.state.companies);
                this.setState({companies: companies.concat(response.data._embedded.companies)});
            } else {
                console.log(response);
            }
        });
    };

    render() {
        return (
            <div className={"wrapperDiv"}>
                {JSON.stringify(this.props.rootState)}
                <div className={"flexDivCol"}>
                    <div id="header">
                        <Header/>
                    </div>
                    <div id="result">
                        <div className={"search"}>
                            <h2>Harjoittelupaikkoja</h2>
                            <p className={"secondaryColor"}>{this.state.companies.length} paikkaa löydetty</p>
                        </div>
                        <div className={"filters"}>
                            <h5 style={{marginTop: '30px', marginBottom: '10px'}} className={"primaryColor"}>
                                Hakukriteerit</h5>
                            <div className={"filter"}>Ravintola- ja cateringala</div>
                            <div className={"filter"}>Tarjoilija</div>
                            <div className={"filter"}>Kaikki</div>
                        </div>
                        <div className={"searchResults"}>
                            <h5 style={{marginTop: '30px', marginBottom: '10px'}} className={"primaryColor"}>
                                Hakutulokset</h5>

                            <InfiniteScroll
                                pageStart={0}
                                loadMore={ this.fetchCompanies }
                                hasMore={true || false}
                                loader={<div className="loader" key={0}>Loading ...</div>}
                                useWindow={false}
                            >
                                {
                                    this.state.companies.map((traineeship, key) => (
                                        <div id={"item"} key={key}>
                                            <div className={"companyInfo"}>
                                                <div className={"heading"}>
                                                    <div id={"companyDiv"}>
                                                        <p style={{
                                                            fontSize: '18px',
                                                            lineHeight: '18px'
                                                        }}>{traineeship.name}</p>
                                                    </div>
                                                    {
                                                        traineeship.video == null
                                                            ? ''
                                                            :
                                                            <div id={"videoDiv"}>
                                                                <div className={"youtubeBox center"}>
                                                                    <div id={"youtubeIcon"}>
                                                                        <a className={"primaryColor"}
                                                                           href={traineeship.mediaUrl}>
                                                                        <span style={{marginRight: '3px'}}><Image
                                                                            src="http://www.stickpng.com/assets/images/580b57fcd9996e24bc43c545.png"
                                                                            style={{
                                                                                width: '24px',
                                                                                height: '17px'
                                                                            }}/></span>
                                                                            <span> <p style={{
                                                                                fontSize: '13px',
                                                                                lineHeight: '18px',
                                                                                margin: 0,
                                                                                display: 'inline-block'
                                                                            }}>Esittely</p></span>
                                                                        </a>
                                                                    </div>
                                                                    <div id={"txtVideo"}>

                                                                    </div>
                                                                </div>
                                                            </div>
                                                    }

                                                </div>
                                                <div className={"location"}>
                                                    <div id={"locationIcon"}>
                                                        <Image src="assets/img/icLocation.png"
                                                               style={{marginTop: '-7px'}}/>
                                                    </div>
                                                    <div id={"address"}>
                                                        <a href={"https://www.google.com/maps/search/?api=1&query=" + encodeURI("Fredrikinkatu 4, Helsinki")}>
                                                            <p className={"primaryColor"}
                                                               style={{fontSize: '13px'}}>{traineeship.city}(show in
                                                                map)</p>
                                                        </a>
                                                    </div>
                                                </div>
                                                <div className={"companyDescription"}>
                                                    <p className={"secondaryColor"} style={{
                                                        fontSize: '14px',
                                                        lineHeight: '20px'
                                                    }}>{traineeship.description}</p>
                                                </div>

                                                <div className={"companyContacts"} style={{marginTop: '20px'}}>
                                                    <p className={"contactInfo"}>URL: {traineeship.website}</p>
                                                    <p className={"contactInfo"}>Email: {traineeship.email}</p>
                                                    <p className={"contactInfo"}>Puh: {traineeship.phonenumber}</p>
                                                    <p className={"contactInfo"}>Contact: {traineeship.contact}</p>
                                                </div>
                                            </div>
                                        </div>
                                    ))
                                }
                            </InfiniteScroll>


                        </div>
                    </div>
                </div>
            </div>
        );
    }
}

export default Traineeship;

问题在于它一直在永久地调用fetchCompanies。有没有解决这个问题?

在文档中没有关于loadmore函数应该如何的示例。

2 个答案:

答案 0 :(得分:0)

问题,渲染方法中的调用函数

loadMore={ this.fetchCompanies }

在componentDidMount中调用函数并在模板中使用this.state打印后设置状态

答案 1 :(得分:0)

“ hasMore”在您的情况下始终为true,在没有下一个URL时将其设置为false。