反应获取API数据到组件

时间:2018-12-30 20:02:18

标签: javascript reactjs api components fetch

我正在尝试从The Movie数据库API制作一个应用程序。 我遇到一个小问题。 我有两个组成部分。首先,我使用fetch,并对Card组件使用map()函数,我想在其中显示api中的数据。如何正确连接它们?

https://codesandbox.io/s/p3vxqqz53q

渲染列表的第一个组件:

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

class ListApp extends Component {
    constructor(props){
        super(props);
        this.state = {
            items: [],
            isLoaded: false,
        }
    };
    componentDidMount = () => {
        fetch("https://api.themoviedb.org/3/movie/popular?api_key=xxxxxxxx&page=1")
        .then(resp => resp.json())
        .then(resp => {
            this.setState({
                isLoaded: true,
                items: resp.results
            })
            console.log(this.state.items)      
    })};

    render() {
        var {isLoaded, items} = this.state;
        return (
            <div>
                {items.map( () => ( <Card/> ) )};
            </div>
        );
    }
}

export default ListApp;

第二部分卡:

import React from 'react';

const Card = (items) => { 
        return (
            <div className="movie-container">
                <img src="https://image.tmdb.org/t/p/w185/{items.poster_path}" alt="NO PHOTO" className="movie-container__img" />
                <div className="movie-container__about">
                    <span className="movie-container__percent">{items.vote_average}</span>
                    <h2 className="movie-container__title">{items.original_title}</h2>
                    <p className="movie-container__date">{items.release_date}</p>
                    <p className="movie-container__text">{items.overview}</p>
                    <a href="https://www.themoviedb.org/movie/" className="movie-container__more">MORE</a>
                </div>
            </div>
            )
}

export default Card;

2 个答案:

答案 0 :(得分:1)

您需要将item对象作为道具传递给Card组件,例如

{items.map(item => <Card key={item.id} item={item} /> )}

,然后从

之类的Card组件中访问item
const Card = (props) => {
  const {item} = props;
  ...

}

答案 1 :(得分:0)

此代码应该有效。

ListApp中@Aakash建议的地图:

 render() {
    var { isLoaded, items } = this.state;
    return (
      <div>
        {items.map(item => (<Card key={item.id} item={item} />))};
            </div>
    );
  }

正确引用道具道具的卡片:

// Card.js
import React from 'react';

const Card = (props) => {
    const { item } = props;
    return (
        <div className="movie-container">
            <img src="https://image.tmdb.org/t/p/w185/{items.poster_path}" alt="NO PHOTO" className="movie-container__img" />
            <div className="movie-container__about">
                <span className="movie-container__percent">{item.vote_average}</span>
                <h2 className="movie-container__title">{item.original_title}</h2>
                <p className="movie-container__date">{item.release_date}</p>
                <p className="movie-container__text">{item.overview}</p>
                <a href="https://www.themoviedb.org/movie/" className="movie-container__more">MORE</a>
            </div>
        </div>
    )
}

export default Card;