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

141
Vistas
React/JS How would I assign a specific mapped element to a state?

My search component returns a mapped list of results based on the input. Depending on the list element clicked, I want to assign it to my current stock state but I am not sure how I would access that specific element.

const selectStock = ()=>{
    setSearch("");
    setCurrentStock();
  }

return (
  <div className="searchContainer">
    <form className="searchBar">
      <input
        className="search"
        type="text"
        placeholder="Search Ticker Symbol"
        onChange={handleSearch}
      />
    </form>
    <div className="searchResults">
      <ul>
        {/* {stockNames.map((stockname) => {
        const { name, symbol} = stockname;
        return (
            <li className="displaySearch" onClick={selectStock} key={symbol}>
                <p>{name} ({symbol})</p>
            </li>
        );
      })} */}
      </ul>
    </div>
  </div>
);

}

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

0

First, you'll want to change your selectStock function to accept the stockName to which it should be set. That should be passed to your setCurrentStock function:

const selectStock = (nextStock) => {
  setSearch("");
  setCurrentStock(nextStock);
}

Then, pass an arrow function through to the onClick function for the row that you select:

<div className="searchResults">
  <ul>
    {stockNames.map((stockname) => {
    const { name, symbol } = stockname;
    return (
        <li className="displaySearch" onClick={() => selectStock(stockname)} key={symbol}>
            <p>{name} ({symbol})</p>
        </li>
    );
  })}
  </ul>
</div>

What this does is essentially create a new function for each row that's specific to itself. When that row is clicked, it triggers its own unique onClick, which passes its own stockname value through to selectStock.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can pass the selectedStock into the selectStock function by making it a ES6 function.

See below:

const selectStock = (selectedStock)=>{
setSearch("");
setCurrentStock(selectedStock);
}
return (
  <div className="searchContainer">
    <form className="searchBar">
      <input
        className="search"
        type="text"
        placeholder="Search Ticker Symbol"
        onChange={handleSearch}
      />
</form>
<div className="searchResults">
  <ul>
    {/* {stockNames.map((stockname) => {
    const { name, symbol} = stockname;
    return (
        <li className="displaySearch" onClick={()=>selectStock(stockName)} key={symbol}>
            <p>{name} ({symbol})</p>
        </li>
    );
  })} */}
      </ul>
    </div>
  </div>
);

You can pass other variables in the similar way if required.

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