1

I am building a Film grid that should return their Id, thumbnail, title, episode number and released date.

How can a map these values of Swapi? (Thumbnails are in img folder)

ListFilms.js Component:

import React, { Component } from "react";

class ListFilms extends Component {
  render() {
    const films = this.props.films;
    console.log(films);
    return (
      <div className="row">
        <table className="table table-dark">
          <thead>
            <tr>
              <th scope="col">Id</th>
              <th scope="col">Thumbnail</th>
              <th scope="col">Film Title</th>
              <th scope="col">Released Date</th>
              <th scope="col">Episode Number</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <th scope="row">{films.id}</th>
              <td>
                <img src={} alt={}>
              </td>
              <td>{films.title}</td>
              <td>{films.created}</td>
              <td>{films.episode_id}</td>
            </tr>
          </tbody>
        </table>
      </div>
    );
  }
}

export default ListFilms;

CodeSandbox Demo

1 Answer 1

1

You can use the below code to iterate through the list of items and get it displayed. I have moved the images to public folder to avoid webpack from having to bundle them as part of your code.

https://codesandbox.io/s/z6y768y37p

<tbody>
        { films.map((film,index) => (<tr key={film.title}>
          <th scope="row">{index + 1}</th>
          <td><img src={`/img/${film.title}.jpeg` width="40px"} /></td>
          <td>{film.title}</td>
          <td>{film.release_date}</td>
          <td>{film.episode_id}</td>
        </tr>)) }
</tbody>
Sign up to request clarification or add additional context in comments.

3 Comments

I have updated the code @mdiiorio. Missed the Ids column previously. great catch!
I need a better reputation for upvote you :S. If you upvote me first i could do that next!
Thks!! I made an update in Films.js, setting the key values for fix the Id's!

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.