Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

82
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda