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

145
Views
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 answers
Answer question

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 Report

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 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!