skip to Main Content

I have built a little movie database and want to display movies to my watchlist. I can search for movies, but when I want to add them to my watchlist, it only gets rendered in my Homescreen component, not in my WatchList component. This is how it looks and what I tried so far. HomeScreen is passing the movies as props to the MovieList component. It will show me all the movies I am searching for. When I now click on the "Add to Watchlist" button, I want the movie to be added to my watchlist (WatchList component). But so far the WatchList component is empty, the movies will only show on my HomeScreen component.

import React from 'react'
import { useEffect, useState } from 'react';
import "./HomeScreen.css"
import MovieList from './componnents/MovieList';
import Header from './componnents/Header';
import AddWatchlist from './componnents/AddWatchlist';
import WatchList from './componnents/WatchList';

function HomeScreen() {
    const [movies, setMovies] = useState([]);
    const [watchList, setWatchList] = useState([]);
    const [searchValue, setSearchValue] = useState('');
  
    const getMovieRequest = async (searchValue) => {
      const url = `http://www.omdbapi.com/?s=${searchValue}&apikey=93fe1299`;
      const response = await fetch(url);
      const responseJson = await response.json();
  
      if (responseJson.Search) {
        setMovies(responseJson.Search);
      }
    };
  
    useEffect (() => {
      getMovieRequest(searchValue);
    }, [searchValue]);

    const addToWatchlist = (movie) => {
      const newWatchList = [...watchList, movie];
      setWatchList(newWatchList);
    };

  return (
    <div className="HomeScreen">
        <Header searchValue={searchValue} setSearchValue={setSearchValue}/>
    <div className='movie__row'>
        <MovieList movies={movies} handleWatchlist={addToWatchlist} watchlistComponent={AddWatchlist}/>
    </div>
    <div className='movie__row'>
        <WatchList watchList={watchList}/>
    </div>
  </div>
  );
}

export default HomeScreen
import React from 'react'
import "./MovieList.css"

const MovieList = (props) => {
  const WatchlistComponent = props.watchlistComponent;
  return (
    <>
    {props.movies.map((movie, index) =>
      <div className='image__container'>
        <img src={movie.Poster} alt='movie'></img>
        <div onClick={()=> props.handleWatchlist(movie)} className='add__watchlist'>
          <WatchlistComponent/>
        </div>
      </div>
     )}
    </>
  )
}

export default MovieList;
import React from 'react'
import "./WatchList.css"

function WatchList({watchList}) {
  if (!watchList) {
    return null;
}

  return (
    <div className='watchlist'>
    <h2>Watch List</h2>
    {watchList.map((movie) => (
      <img src={movie.Poster} />
    ))}
  </div>
);
}

export default WatchList

2

Answers


  1. If add mock value in app component, for example:

    const [movies, setMovies] = useState([
        {
          Poster: "http://www.clker.com/cliparts/R/S/Z/4/t/f/crossed-hammers-bw-100x100-md.png",
          text: "hello",
        },
      ]);
    

    and rewite component MovieList

    const MovieList = (props) => {
      return (
        <>
          {props.movies.map((movie, index) => (
            <div className="image__container">
              <img src={movie.Poster} alt="movie"></img>
              <div onClick={() => props.handleWatchlist(movie)} className="add__watchlist">
                {movie.text}
              </div>
            </div>
          ))}
        </>
      );
    };
    

    And rewrite component WatchList

    function WatchList({ watchList }) {
      if (!watchList) {
        return null;
      }
    
      return (
        <div className="watchlist">
          <h2>Watch List</h2>
          {watchList.map((movie) => (
            <>
              <img src={movie.Poster} />
              {movie.text}
            </>
          ))}
        </div>
      );
    }
    

    Now when I click on text "Hello" which is above the words "Watch List", I get copy this "movie", and it appears below the phrase "Watch List".

    This is what you need?

    Login or Signup to reply.
  2. I think you go wrong in the watchlist component. But can you console log what data you get when you press to actually add the movie to the watchlist? Also i would try something like this maybe

    import React from 'react';
    import "./WatchList.css";
    
    function WatchList({ watchList }) {
      if (watchList.length === 0) {
        return (
          <div className='watchlist'>
            <h2>Watch List</h2>
            <p>Your watchlist is empty.</p>
          </div>
        );
      }
    
      return (
        <div className='watchlist'>
          <h2>Watch List</h2>
          {watchList.map((movie) => (
            <img src={movie.Poster} alt={movie.Title} key={movie.imdbID} />
          ))}
        </div>
      );
    }
    
    export default WatchList;
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search