Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

134
Vistas
is there a way of passing the cars component as the search result from the search inputs in React.js?

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.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Okay, there are several ways to do this.

  1. You can create a parent element in which you declare a state, for example filtered, which you will pass to searchComponent where it will change, and in carComponent you will pass the already filtered state:

Like this:

 const Parent () => {
   const [filtered, setFiltered] = useState('');
   const handleFilter = (data) => {
          setFiltered(data)
   }
        
   return (<>
     <Search setFiltered={handleFilter}/>
     <Cars data={filtered}/>
   </>)
}

  1. You can create custom hooks with state, put them in a separate file, and use their state where you want, although I'm not entirely sure about this approach, because. I myself have been using more advanced methods for a long time.

That's about custom hooks: https://reactjs.org/docs/hooks-custom.html

  1. Use libraries for state management like MobX or Redux
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda