Skip to content
Advertisement

How to update array of a component from another component’s function?

I am rebuilding a static website in react https://movie-list-website-wt.netlify.app/ I am trying to transfer the search function to react. My current search function works as intended, it returns an array of movies that is being searched, I want it to update the data fetched by the movie cards so that when I search a query, the movie cards use the returned search data instead of the original one this is my App.js file

JavaScript

and this is part of my Top navigation component that includes the search bar and function

JavaScript

Advertisement

Answer

If you need to filter the movies array in the Topnav component you can just pass the setMovies state function as prop to the component.
All you need to do is update the data in the onChange method:

App.js

JavaScript

Topnav.js

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement