JS-201-ReactFundamentals
  • Part 0: App Overview
  • Part 1: Intro to React
    • 1.0: Packages Setup
    • 1.1: Project Setup
    • 1.2: React Router Dom
  • Part 2: JavaScript Concepts
    • 2.0: ES6 Overview
    • 2.1: classes
    • 2.2: constructors
    • 2.3: extends
    • 2.4: super
    • 2.5: interpolation
    • 2.6: map
    • 2.7: filter
  • Part 3: Functional Components
    • 3.0: Functional Components Overview
    • 3.1: Calling Functional Components
    • 3.2: Single Element Rule
    • 3.3: Arrow Functions
    • 3.4: Challenge
    • 3.4: Solution
    • 3.5: Challenge 2
    • 3.5: Solution 2
  • Part 4: JSX Challenge
    • 4.1: JSX Overview
    • 4.1: Challenge Answer
    • 4.2: className
  • Part 5: Class Concepts
    • 5.1: State
    • 5.2: setState
    • 5.3: Class Components Challenge
    • 5.4: Class Components Challenge Answer
  • Part 6: Props Starter
    • 6.0: Props Overview
    • 6.1: Props Demo and Challenge 1
    • 6.2: Props Answer 2
    • 6.3: Props Passing Functions and Challenge 2
    • 6.4: Props Answer 2
    • 6.5: External Props and Mapping Components
    • 6.6: PropTypes
  • Part 7: Lifecycle Methods
    • 7.0: Lifecycle Overview
    • 7.1: Lifecycle Methods
    • 7.2: Mounting Methods
    • 7.3: Update Methods
    • 7.4: Unmount Methods
  • Part 8: Apps
    • 1.0 - Small Timer Apps
      • 1.1 - Simple Timer
      • 1.2 - Clock App
      • 1.3 - Stop Watch App
      • 1.4 - Challenge
    • 2.0 - Concept List
      • 2.1 - Concept Data
      • 2.2 - Concept App Component
      • 2.3 - Concept Component
      • 2.4 - Concept List Component
    • 3.0 - NYT
      • 3.1 - NytApp Component
      • 3.2 - Nyt Results
    • 4.0 - The Friend List App
      • 4.1 - Friend List App Component
      • 4.2 - Friend Component
    • 5.0 - Movie Search Application
      • 5.1 - Form Component
      • 5.2 - Form Results Component
      • 5.3 - Styled Components
    • 6.0 - YouTube Api
      • 6.1 - Video Component
      • 6.2 - Video Component
      • 6.3 - Video Component
    • 7.0 - Github Api Application
      • 7.1 - The Users
      • 7.2 - Github API Component
      • 7.3 - Github API Card
      • 7.4 - Github API Card Form
      • 7.5 - Github API Card List
      • 7.6 - Github API Search
      • 7.7 - Github API Challenge
    • 8.0 - Bitcoin Api Application
      • 8.1 - Bitcoin API Setup
      • 8.2 - Bitcoin API Fetch
      • 8.3 - Bitcoin API Line Chart
      • 8.4 - Bitcoin API Fetching Data
      • 8.5 - Bitcoin API Info Box
      • 8.6 - Bitcoin API Completed Code
    • 9.0 - Google Maps Api Challenge
    • 10.0 - Sound Cloud App Challenge
    • 11.0 - VR App Challenge
    • 12.0 - React Native Intro
  • Part 9: Project
  • Part 10: Notes
    • 10.1 - Resources
    • 10.2 - Troubleshooting
Powered by GitBook
On this page
  • Results Format
  • FormResults
  • Finished code:
  • Form Component
  1. Part 8: Apps
  2. 5.0 - Movie Search Application

5.2 - Form Results Component

In the last section, we set up our Form component, but now we need to create our next component FormResults. This component will be where use to display the results from our API query.

Let's just get started by setting up our component. We need to make sure that it's displaying in our app. Go into the FormResults.js file. Since we just need this component to display our form results, we're not requiring access to state or anything, we can make this a functional component.

import React from 'react';

const FormResults = () => {
    return (
        <h1> Results </h1>
    );
}
export default FormResults;

We need to remember to put the props we are sending to this component in, your component should look like this:

import React from 'react';

const FormResults = ({results}) => {
    return (
        <h1> Results</h1>
    );
}
export default FormResults;

Results Format

Now that we have our results available to use, we can start working on formatting our results to display them. We can make an unordered list of all of our results, and give it an ID of "results". It's important that this ID is the same because it's used in our parent component. Go ahead and make your return statement look like this one:

    return (
        <ul id="results">
            {resultsFormatted}
        </ul>
    );

To challenge yourself, see if you can set up this map before looking at the code below. Go through it step by step! This is an example image of what it should look like when done. Scroll down to see the finished code for this component when you're done. Here we show one of Carolyn's favorite movies:

FormResults

We also need to call the component. Remember that this is a component that we can pass the results to, that will handle displaying them. Go back to the Form.js file and make your render() look like this:

    render() {
        return (
            <form onSubmit={this.handleSubmit} id="form">
                <input onKeyUp={this.handleKeyUp} id="searchInput" className="searchBar" type="text" placeholder="Search a movie" required />
                {this.state.results === [] ? <div></div> : <FormResults results={this.state.results} /> }
            </form>
        );
    }

We also need to import FormResults so that we can use it. Add the following import line to the top of the file.

import FormResults from './FormResults';

Finished code:

Below is the finished code for this component. If you were able to get anything similar, great job!!

import React from 'react';

const FormResults = ({results}) => {
    const link = 'https://image.tmdb.org/t/p/w300';
    const resultsFormatted = results.map((element, index) =>
        <li key={index}>
            <img src={results[index].poster_path === null ? 'http://via.placeholder.com/640x960' : `${link}${results[index].poster_path}`} alt={`${results[index].title} poster`} className="resultPoster" />
            <div>
                <p>{results[index].title}</p>
                <p>{results[index].release_date}</p>
            </div>
        </li>
        );
    return (
        <ul id="results">
            {resultsFormatted}
        </ul>
    );
}
export default FormResults;

Form Component

Here is Form.js with new editions:

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

export class Form extends Component {
  constructor(props) {
    super(props);
    this.state = {
      results: []
    };
  }

  handleSubmit = e => {
    e.preventDefault();
  };

  handleKeyUp = (e) => {
    const key = "f8ce157750fba5e5de7193255f318905";

    fetch(
      `https://api.themoviedb.org/3/search/movie?api_key=${key}&language=en-US&query=${e.target.value}&page=1&include_adult=false`
    )
      .then(response => {
        if (response.status !== 200) {
          console.log("Error: " + response.status);
          return;
        }

        response.json().then(data => {
          const results = data.results;
          this.setState({ results });
        });
      })

      .catch(err => {
        console.log("Fetch Error :-S", err);
      });
  };

  render() {
    return (
      <form onSubmit={this.handleSubmit} id="form">
        <input
          onKeyUp={this.handleKeyUp}
          id="searchInput"
          className="searchBar"
          type="text"
          placeholder="Search a movie"
          required
        />
        {this.state.results === [] ? (
          <div />
        ) : (
          <FormResults results={this.state.results} />
        )}
      </form>
    );
  }
}
Previous5.1 - Form ComponentNext5.3 - Styled Components

Last updated 7 years ago

Now, we just need to figure out how to format our results for display! We're going to use map again to take our results and turn them each into a list item with all the information we want. There is a way to grab movie posters for each movie with The Movie Database API, and we're going to use it to display movie posters with our movies! You can read about it in the docs . We're going to take our results and map them into list items with the following things: 1. unique keys 2. an img tag with the movie poster URL 3. the name of the movie 4. the release date

Next, we're going to talk about a fun way to style react components, called styled-components!!!

here
Styling our Movie App
beauty