1

每次向下滚动时,我都使用这个库 react-infinite-scroller 来加载更多项目,但由于某种原因,我没有触发 loadmore。

代码如下所示:

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) });
                // this.setState({companies: this.state.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,那么它会无限地调用该函数,即使每次调用页面都会不断增加,这很好,但问题是它会一直调用无限,甚至没有滚动!

知道如何解决这个问题吗?

4

1 回答 1

0

请使用以下代码更改您的代码:

 <InfiniteScroll pageStart={0}
                 loadMore={this.fetchCompanies}
                  hasMore={true || false}
                  loader={<div className="loader" key={0}>Loading ...</div>}
                  useWindow={false}>
于 2020-10-01T10:03:11.910 回答