0

I am building a Film grid that return their Id, thumbnail, title, episode number and released date. The onClick events of "Details" are already binded to the :id_film url.

How can I display the correct Film Object Values on each specific view e.g: all species.names?

CodeSandbox Demo & Api Documentation Swapi

DetailFilms.js Component:

import React, { Component } from 'react';
import axios from 'axios';
import { Link } from 'react-router-dom';

class DetailFilms extends Component {
  state = {
    film: null
  }
  componentDidMount() {
    let id = this.props.match.params.films_id;
    axios.get('https://swapi.co/api/films/' + id)
      .then(res => {
        this.setState({
          film: res.data
        })
        console.log(res)
      })
  }
  render() {
    const film = this.state.film ? (
      <div className="jumbotron">
        <h2 className="display-4">{this.state.film.title}</h2>
        <p className="lead">{this.state.film.director}</p>
        <hr className="my-4" />
        <p>{this.state.film.producer}</p>
        <p>Episode Nº: {this.state.film.episode_id}</p>
        <p className="font-italic">{this.state.film.opening_crawl}</p>
        <p>Species: {this.state.film.species}</p>
        <Link className="btn btn-primary btn-lg" to="/" role="button">Learn more</Link>
      </div>
    ) : (
      <div className="spinner-border" role="status">
        <span className="sr-only">Loading...</span>
      </div>
    )
    return (
      <div className="d-flex justify-content-center align-items-center ">
        {film}
      </div>
    )
  }
}

export default DetailFilms;
6
  • 1
    in your render you are calling this.state.films with an 's' when you set your state and make your get request you are using film. That is one issue Commented Feb 4, 2019 at 17:07
  • 1
    Friendly advice, I assume you are just learning react, so don't use componentWillMount for any API request componenentDidMount is a much better solution! Commented Feb 4, 2019 at 17:15
  • @MarioRozic why? If I remplace the method it stop working. Commented Feb 4, 2019 at 18:39
  • @mdiiorio I know, daveceddia.com/…, check this link, 2 min read! Commented Feb 4, 2019 at 18:42
  • @MarioRozic thanks fort that! I recently found that in DetailFilms.js I passed the wrong film data. You know how can i fix this? Thks! Commented Feb 4, 2019 at 23:25

1 Answer 1

3

First your render method is calling

this.state.films

You want

this.state.film

Also in your get request you are setting this.state.film = res.data.results there is no "results" on this api. Remove it and just use:

this.setState({
    film: res.data
})

This works in your code sandbox.

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

3 Comments

Thks! but how can y acces to an array object value again like species names?
@GifCo I recently found that in DetailFilms.js I passed the wrong film. You know how can i fix this? Thks!
@modiiorio im not sure i follow? The best thing to do is console.log(res) in your axios.get calls. That way you can see the entire object in Chrome dev tools and see how it is structured. IF you mean you click on a move for more details and you get a different movies details, you must be passing the wrong movie ID in your URL params

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.