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

80
Views
How to set sessionStorage dynamically from Object mapping in react

I have an array of objects named 'rounds' which I am mapping through in React. I am rendering an image in a gallery for each object/index in the array. I want to have the option to assign each image rendered, with it's original object key/value pairs. So, if I select the first image from the gallery, the object key/values are set as those of the index[0] of the array. Selecting the second image will set the key/values of the object to those of index[1] of the array and so on. I have considered setting these values as a single object stored in sessionStorage, so that the image selected (and its key/values) can be further processed within another react component namely 'ReviewSingleRound'.

What I have tried so far, I have wrapped the image in a in an attempt to only set the values associated with the selected image with the onClick event however, I can see that the session storage value is set on rendering the gallery. It is not setting with the onClick event. Another problem with this is that each image, has the same item values as each other, that is the last object in the original object array.

Is there a better way to render the gallery and then have it that the images are attributed with only the key/values of the correspinding object from the rounds array?

Here is the return and map section of my code:

return(
    <div>
    <h3 className='heading'> Review Rounds</h3>
    <Container className='gallery' >
      <div className='row gallery-row'>
        {
          rounds.map((roundsItem,index)=>{
            return(
              <div key={index} className='col-6 col-md-4'>
               <button onClick={sessionStorage.setItem("image", JSON.stringify(rounds[index]))}><a href="/ReviewSingleRound"> <img src={image} 
                />
                <br />Title
                </a>
               </button>
              </div>
            )
          })
        }
      </div>
    </Container>
    </div>
  )
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

you are passing undefined to onClick:

onClick={sessionStorage.setItem("image", JSON.stringify(rounds[index]))}

you should pass an function that will eventually set some value in localStorage

onClick={() => sessionStorage.setItem("image", JSON.stringify(rounds[index]))}

Answer to second problem is that you can create as many keys as you wish - to store multiple objects

onClick={() => sessionStorage.setItem(`image-${index}`, JSON.stringify(rounds[index]))}
about 4 years ago · Santiago Trujillo 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!