2017-12-29 22 views
1

私は現時点で2番目のプロジェクトに反応して作成しています。 MovieDb APIを使用して映画検索アプリを作成しようとしています。私が映画の最初のリストを取得すると、すべてのことが大丈夫です。しかし、それぞれのリスト項目をonClickして、各ムービーの詳細を表示したい。私はバニラJSと伝統的なXHR呼び出しを使用して、このようないくつかのアプリケーションを作成しました。今回は私が使っている簡単なAPIのようなfetch APIを使用していますが、レスポンスデータをマップして各ムービーのIDを取得し、それぞれの詳細を取得するには、すべてのアイテムの詳細の完全なリストを取得します。これは所望の効果ではない。マップのsetStateの後には、最後の要素の詳細しか取得していなかったので、オブジェクトのリストを配列に配置しました。私はおそらくAPIコールの中で何か間違っていることを知っていますが、それは私のREACTコード全体であるかもしれません。助けていただければ幸いです。リバースセットステートフェッチAPI

私のコード

App.jsは

import React, { Component } from 'react'; 
import SearchInput from './Components/SearchInput' 
import './App.css'; 

class App extends Component { 
    constructor(props) { 
    super(props); 

     this.state = 
     { 
      value: '', 
      showComponent: false, 
      results: [], 
      images: {}, 
     }; 

    this.handleSubmit = this.handleSubmit.bind(this); 
    this.handleOnChange = this.handleOnChange.bind(this); 
    this.getImages = this.getImages.bind(this); 
    this.getData = this.getData.bind(this); 
} 

ComponentWillMount() { 
    this.getImages(); 
    this.getData(); 
} 

getImages(d) { 
    let request = 'https://api.themoviedb.org/3/configuration?api_key=70790634913a5fad270423eb23e97259' 
    fetch(request) 
    .then((response) => { 
     return response.json(); 
    }).then((data) => { 
     this.setState({ 
     images: data.images 
     }); 
    }); 
} 

    getData() { 
    let request = new Request('https://api.themoviedb.org/3/search/movie?api_key=70790634913a5fad270423eb23e97259&query='+this.state.value+''); 

    fetch(request) 
     .then((response) => { 
     return response.json(); 
     }).then((data) => {    
     this.setState({ 
      results: data.results 
     });    
     }); 
    } 

    handleOnChange(e) { 
    this.setState({value: e.target.value}) 
    } 

    handleSubmit(e) { 
    e.preventDefault(); 
    this.getImages(); 
    this.setState({showComponent: true}); 
    this.getData(); 
    } 

    render() { 
    return (
     <SearchInput handleSubmit={this.handleSubmit} handleOnChange={this.handleOnChange} results={this.state.results} images={this.state.images} value={this.state.value} showComponent={this.state.showComponent}/> 
    ); 
    } 
} 

export default App; 


SearchInput.js 

import React, {Component} from 'react'; 
import MoviesList from './MoviesList'; 

class SearchInput extends Component { 
    render() { 
    return(
     <div className='container'> 
     <form id='search-form' onSubmit={this.props.handleSubmit}> 
      <input value={this.props.value} onChange={this.props.handleOnChange} type='text' placeholder='Search movies, tv shows...' name='search-field' id='search-field' /> 
      <button type='submit'>Search</button> 
     </form> 
     <ul> 
      {this.props.showComponent ? 
       <MoviesList value={this.props.value} results={this.props.results} images={this.props.images}/> : null 
      } 
     </ul> 
     </div> 
    ) 
    } 
} 

export default SearchInput; 

これは私が

import React, { Component } from 'react'; 
import MovieDetails from './MovieDetails'; 

let details = []; 

class MoviesList extends Component { 
    constructor(props) { 
    super(props); 

    this.state = { 
     showComponent: false, 
     details: [] 
    } 

    this.showDetails = this.showDetails.bind(this); 
    this.getDetails = this.getDetails.bind(this); 

    } 


    componentDidMount() { 
    this.getDetails(); 
    } 

    getDetails() { 
    let request = new Request('https://api.themoviedb.org/3/search/movie?api_key=70790634913a5fad270423eb23e97259&query='+this.props.value+''); 

    fetch(request) 
     .then((response) => { 
     return response.json(); 
     }).then((data) => {  
     data.results.forEach((result, i) => { 
     let url = 'https://api.themoviedb.org/3/movie/'+ result.id +'?api_key=70790634913a5fad270423eb23e97259&append_to_response=videos,images'; 
     return fetch(url) 
     .then((response) => { 
      return response.json(); 
     }).then((data) => { 
      details.push(data) 
      this.setState({details: details}); 
     }); 
     }); 
     console.log(details); 
    }); 
} 



    showDetails(id) { 
    this.setState({showComponent: true},() => { 
     console.log(this.state.details) 
    }); 
    console.log(this.props.results) 
    } 

    render() { 
    let results; 
    let images = this.props.images; 

    results = this.props.results.map((result, index) => { 
     return(
     <li ref={result.id} id={result.id} key={result.id} onClick={this.showDetails}> 
      {result.title}{result.id} 
      <img src={images.base_url +`${images.poster_sizes?images.poster_sizes[0]: 'err'}` + result.backdrop_path} alt=''/> 
     </li> 
    ) 
    }); 

    return (
     <div> 
     {results} 
     <div> 
      {this.state.showComponent ? <MovieDetails details={this.state.details} results={this.props.results}/> : null} 
     </div> 
     </div> 
    ) 
    } 
} 

export default MoviesList; 

MovieDetails.jsを詳細データをフェッチ

MovieList.jsしようとするコンポーネントです

import React, { Component } from 'react'; 

class MovieDetails extends Component { 


    render() { 
    let details; 
    details = this.props.details.map((detail,index) => { 
     if (this.props.results[index].id === detail.id) { 
     return(
     <div key={detail.id}> 
      {this.props.results[index].id} {detail.id} 
     </div> 
    )} else { 
     console.log('err') 
     } 
    }); 

    return(
     <ul> 
     {details} 
     </ul> 
    ) 

    } 
} 

export default MovieDetails; 
+0

また、state.detailsは検索ごとに常に同じであることを忘れています。他のすべての状態は新しい検索で変更されますが、state.detailsは常に同じ – AndrewB

+0

のままです。あなたの記事にあなたのapiキーを含めないことをお勧めします... –

+0

実際の問題は何かを明確にすることはできますか?問題の原因は何ですか?特にapiは呼び出しますか?また、クライアント側でAPI呼び出しを行う場合は、[axios](https://github.com/axios/axios)をご覧ください。 –

答えて

0

はあなたのonclickリスナーを付けるだろうし、あれば、あなたのいずれかを介して送信する「この特定のムービー機能の詳細を取得」、ID、または完全な結果を解雇でしょう...ここここ

//が起こって多くのことをtheresのあなたは欲しい。

// getDetailsは、1つのムービーを取得するために使用できる引数(ID)を必要とします。

getDetails(id){ 
fetch(id) 
displayresults, profit 
} 

results = this.props.results.map((result, index) => { 
      return(
      <li onClick={() => this.getDetails(result.id) ref={result.id} id={result.id} key={result.id} onClick={this.showDetails}> 
       {result.title}{result.id} 
       <img src={images.base_url +`${images.poster_sizes?images.poster_sizes[0]: 'err'}` + result.backdrop_path} alt=''/> 
      </li> 
     ) 
     }); 
0

すべての回答ありがとうございますが、私は実際に友人の助けを借りてそれを並べ替えるようにしました。私のMovieListでは、私は、各コンポーネントの作品と呼ばれる新しいコンポーネントを返却し、そこに私は、コールがMovieListコンポーネントでマイマップ機能から

Movielist

import React, { Component } from 'react'; 
import Movie from './Movie'; 

class MoviesList extends Component { 
    render() { 
    let results; 
    if(this.props.results) { 
     results = this.props.results.map((result, index) => { 
     return(
      <Movie key={result.id} result={result} images={this.props.images}/> 
     ) 
     }); 
    } 

    return (
     <div> 
     {results} 
     </div> 
    ) 
    } 
} 

export default MoviesList; 

映画映画の詳細のそれぞれを使用して、あちこちのムービーの詳細情報をAPIに作ります.js

import React, { Component } from 'react'; 
import MovieDetails from './MovieDetails'; 

class Movie extends Component { 
    constructor(props) { 
    super(props); 

    this.state = { 
     showComponent: false, 
     details: [] 
    } 

    this.showDetails = this.showDetails.bind(this); 
    this.getDetails = this.getDetails.bind(this); 
    } 

    componentDidMount() { 
    this.getDetails(); 
    } 

    getDetails() { 
    let request = new Request('https://api.themoviedb.org/3/search/movie?api_key=70790634913a5fad270423eb23e97259&query='+this.props.value+''); 

    fetch(request) 
     .then((response) => { 
     return response.json(); 
     }).then((data) => {  
     let url = 'https://api.themoviedb.org/3/movie/'+ this.props.result.id +'?api_key=70790634913a5fad270423eb23e97259&append_to_response=videos,images'; 
     return fetch(url) 
     }).then((response) => { 
      return response.json(); 
     }).then((data) => { 
      this.setState({details: data}); 
     }); 
} 

    showDetails(id) { 
    this.setState({showComponent: true},() => { 
     console.log(this.state.details) 
    }); 
    } 

    render() { 
    return(
     <li ref={this.props.result.id} id={this.props.result.id} key={this.props.result.id} onClick={this.showDetails}> 
     {this.props.result.title} 
     <img src={this.props.images.base_url +`${this.props.images.poster_sizes?this.props.images.poster_sizes[0]: 'err'}` + this.props.result.backdrop_path} alt=''/> 
     {this.state.showComponent ? <MovieDetails details={this.state.details}/> : null} 
     </li> 
    ) 

    } 
} 


export default Movie;