Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

98
Views
passing react hooks into component

I'm new to React. I'm trying to add additional functionality of deleting the record from the list by setting the value.

here is my App.js

import React, { useState } from "react";
import data from "./data";
import List from "./List";

function App() {
  const [movies, setMovie] = useState(data);

  return (
    <main>
      <section className='container'>
        <h3>{movies.length} Movies to Watch</h3>
        <List movies={movies} setMovie />
        <button onClick={() => setMovie([])}>clear all</button>
      </section>
    </main>
  );
}

export default App;

In List.js, Im trying to delete the record when clicking on Watched button. Can I call setMovie inside the List component? is it a correct way?

List.js

import React from "react";

const List = ({ movies }, setMovie) => {
  return (
    <>
      {movies.map((movie) => {
        const { id, name, year, image } = movie;
        return (
          <article key={id} className='person'>
            <img src={image} alt={name} />
            <div>
              <h4>{name}</h4>
              <button
                className='btn'
                onClick={(id) =>
                  setMovie(movies.filter((movie) => movie.id !== id))
                }
              >
                watched
              </button>
              <p>{year}</p>
            </div>
          </article>
        );
      })}
    </>
  );
};

export default List;

enter image description here

almost 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You have two mistakes in your code. First:

<List movies={movies} setMovie />

This shorthand assigns a value of true to setMovie. To assign the setMovie function to it, you must instead do:

<List movies={movies} setMovie={setMovie} />

And secondly this:

const List = ({ movies }, setMovie) => {

Should be this:

const List = ({ movies, setMovie }) => {
almost 4 years ago · Santiago Trujillo Report

0

try:

<List movies={movies} setMovie={setMovie} />

this way the funcition will appear in the List component as a prop.

The way you were doing, it will just appear as true

almost 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!