使用在线API响应无限滚动功能

时间:2018-08-09 12:19:43

标签: javascript jquery reactjs

我正在尝试进行无限滚动并使用React-Waypoint。 我想无限展示{this._renderList() }。 但是它仍然没有用。 我正在使用YTS API,并且似乎具有页面和限制参数...你们对如何无限显示数据有想法吗?

这是yts API的链接 https://yts.am/api#list_movies

import React, {Component} from 'react';
import L_MovieList from './L_MovieList';
import L_Ranking from './L_Ranking';
import './L_Right_List.css';
import Waypoint from 'react-waypoint';
class L_BoxOffice extends Component {
  state = {
    currentPage: 0,
    data : []

  };
  constructor(props) {
    super(props);
    this.state = {
      movies: []
    }
    this._renderRankings = this._renderRankings.bind(this);
    this._renderList = this._renderList.bind(this);
  }
  componentWillMount() {
    this._getMovies();
  }
  _renderRankings = () => {
    const movies = this.state.movies.map((movie, i) => {
      console.log(movie)
      return <L_Ranking title={movie.title_english} key={movie.id} genres={movie.genres} index={i}/>
    })
    return movies
  }
  _renderList = () => {
    fetch(`https://yts.am/api/v2/list_movies.json?sort_by=download_count&page=${this.state.currentPage}`)
      .then((res) => this.setState((prevState) => ({currentPage: prevState.currentPage + 1, data: res.body}));
    const movies = this.state.movies.map((movie) => {
      console.log(movie)
      return <L_MovieList title={movie.title_english} poster={movie.medium_cover_image} key={movie.id} genres={movie.genres} language={movie.language} runtime={movie.runtime} year={movie.year} rating={movie.rating} likes={movie.likes} trcode={movie.yt_trailer_code}/>
    })
    return movies
  }
  _getMovies = async () => {
    const movies = await this._callApi() 
    this.setState({ 
      movies 
    })
  }
  _callApi = () => {
    return fetch('https://yts.am/api/v2/list_movies.json?sort_by=download_count&limit=10').then(potato => potato.json()) 
      .then(json => json.data.movies)
      .catch(err => console.log(err))
  }
  getNextPage = () => {
  fetch(`https://yts.am/api/v2/list_movies.json?sort_by=download_count&page=${this.state.currentPage}`).
    then((res) => this.setState((prevState) => ({currentPage: prevState.currentPage + 1, data: res.body}));
}

  render() {
    const {movies} = this.state;
    let sub_title;
    let right_information;
    if (this.props.page == 'main') {
      sub_title = <div>Today Box Office</div>;
      right_information = <div>
        aaa</div>;
    } else if (this.props.page == 'box_office') {
      right_information = <div className={movies
          ? "L_Right_List"
          : "L_Right_List--loading"}>
        {this._renderList()}
        {
          this.state.data.map((currentData) => <div>{this._renderList()}</div>)
        }
        <Waypoint onEnter={this.getNextPage}/>
      </div>;
    }
    return (<div style={{
        backgroundColor: '#E5E5E5',
        paddingTop: '20px',
        paddingLeft: '20px'
      }}>
      {sub_title}
      <div className={movies
          ? "L_Left_Ranking"
          : "L_Left_Ranking--loading"}>
        <div className="L_Left_Ranking_title">영화랭킹</div>
        {this._renderRankings()}
      </div>
      {right_information}

    </div>);
  }
}

export default L_BoxOffice;

0 个答案:

没有答案