1

In this code, I would like to show data with directly using function _renderMovies not like

{movies? this._renderMovies(): 'Loading!' }

cuz I don't want to show Loadings

Do you guys have an idea of how can I use directly function _renderMovies?

My code :

import React, { Component } from 'react';
import L_MovieList from './L_MovieList';
import L_Ranking from './L_Ranking';
import './L_BoxOffice.css';
class L_BoxOffice extends Component {
  state ={

  }
  constructor(props) {
      super(props);
      this._renderMovies = this._renderMovies.bind(this);
  }
componentDidMount(){
    this._getMovies();
}
_renderMovies=()=>{
  const movies= this.state.movies.map((movie)=>{
    console.log(movie)
      return  <L_Ranking
      title={movie.title_english}
      key={movie.id}
      genres={movie.genres}
      />
  })
  return movies
}
_getMovies = async()=>{
 const movies = await this._callApi() 
 this.setState({
   //movies : movies
   movies 
 })
}
  _callApi=()=>{
    return   fetch('https://yts.am/api/v2/list_movies.json?sort_by=download_count')
    .then(potato=> potato.json()) 
    .then(json=> json.data.movies)
    .catch(err=>console.log(err))
    }
  render() {
      const{movies}=this.state;
    return (
      <div>

      <div className={movies ? "L_BoxOffice" : "L_BoxOffice--loading"}>
      <div className="L_Ranking_title">RANKING</div>
{movies? this._renderMovies(): 'Loading!' }
      </div>
        Box office page
        <L_MovieList/>
      </div>
    );
  }
}

export default L_BoxOffice;

3 Answers 3

1

First of all set movies to be an empty array by default in the state.

constructor(props) {
  super(props);
  this.state = { movies: [] }
  this._renderMovies = this._renderMovies.bind(this);
}

After that just render the movies:

<div className="L_Ranking_title">RANKING</div>
  {this._renderMovies()}
</div>

Having an empty array as a default value, will remove the ternary operator usage and .map will always work, because by default the movies will be iterable.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot ! you helped me! Can I ask one more question? after fixed it, the data comes out late..around 2sec. I want to show date right when open the page. Is that because I put the function in componentDidMount()?.. why _renderMovies returns so late?
In short: it's fine that you call it in componentDidMount(). It won't have too improvement if you call it in the constructor or any other lifecycle method. Please read this article. It depends to your app architecture. You can create a HOC that will be responsible for data fetching, but keep in mind that there will be always a resource loading time, because of the API server response time.
1

Replace {movies? this._renderMovies(): 'Loading!' } with _this.renderMovies()

Comments

0

use arrow function

like you can directly call any function like this

_renderMovies = ()=>{
 if(true){

  }else{
   return 'loading...' //just an example
   }
}

render(){
  {this._renderMovies()}
}

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.