This is the code for my search component, it contains the search input that returns a search result
function Search (props) {
const [search, setSearch] = useState("")
function handleSearch(e){
setSearch(e.target.value)
};
const filtered = !search
? data
: data.filter((car) =>
car.name.toLowerCase().includes(search.toLowerCase())
);
return(
<>
<div className="search-container">
<input
value={search}
onChange={handleSearch}
className="searchbox"
type='text'
placeholder="Find a car.."/>
</div>
</>
)
}
this is my CARS COMPONENT it shows the the collective lists of cars from my data
function Cars (){
return(
<>
<div className="cars-container">
{data.map((car) =>{
return (
<div className="cars-wrapper"
key={car.number}>
<img className='car-image'
src={car.image} alt="mercedez" />
<h4>{car.name}</h4>
<p>{car.model}</p>
<Link style={{fontSize: '1rem'}}
to={`/cars/${car.number}`}>Mind blowing features</Link>
</div>
)
})}
</div>
<Outlet />
</>
)
}
export {Cars}
Now my question is, how can i pass the CARS component to the SEARCH component to be displayed whenever the search input is used to search for a car.
Okay, there are several ways to do this.
Like this:
const Parent () => {
const [filtered, setFiltered] = useState('');
const handleFilter = (data) => {
setFiltered(data)
}
return (<>
<Search setFiltered={handleFilter}/>
<Cars data={filtered}/>
</>)
}
That's about custom hooks: https://reactjs.org/docs/hooks-custom.html