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

148
Visualizações
how do i get an element that had its state changed

I have a situation where once I click an image, I'd like it to get a border. And if i click it again, i'd like that border to be removed. BUT, i also want that border to be removed (or "moved") if another image is clicked instead!

i guess there are a couple of ways to go about this - both of which im not sure how to implement:

  1. clear out all of the images' borders
  2. keep track of previous state and get the element that way

im using states that are passed down from like great-grandparents, so not sure i can post all the scripts - ill try to keep it in snippets instead.

my images: using 'next/image'

    return(
        <div key={attr} id={attrprops.attrChoice+':'+attr}>
        <Image
            src={imgSourceUrl}
            alt={attrprops.alt}
            width={attrprops.w}
            height={attrprops.h}

            onClick={()=>{  attrprops.setButtonClick(!attrprops.buttonClick);
                            attrprops.setBorder(attrprops.selectedAttr==attr ? false : true);
                            attrprops.setSelectedAttr(attr);
                        }}
        />
        <br></br>
        <div className='flex justify-center align-center'>{attr}</div>
        </div>
    );

my state change:

useEffect(()=>{
    if (border&&buttonClick){
        document.getElementById(`body_type:${selectedAttr}`).className = 'border-2 border-blue-500';
        publishprops.setAttrNumber(4);
    }
    else if (!border&&buttonClick){
        document.getElementById(`body_type:${selectedAttr}`).className = 'border:none';
        publishprops.setAttrNumber(2);
    }
},[border])

the way it is currently, it only adds the borders to selected images. really not sure what route to go on this...

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In order to use React effectively, you need to change your thinking about the state a little. Your template should read easily, it should be clear what's the intent.

It looks like your example is pretty simple. It's either some image highlighted, or none.

Consider this simplified example:

import React, { useState } from 'react';

const images = [
    '1.png',
    '2.png',
    '3.png',
    '4.png',
    '5.png',
];

const MyComponent = () => {
    const [selectedImage, setSelectedImage] = useState(null);

    return (
        <ul>
            {images.map(src => (
                <img
                    key={src}
                    className={`
                        my-image
                        ${selectedImage === src ? 'active' : ''}
                    `}
                    onClick={() => setSelectedImage(
                        selectedImage => (
                            selectedImage !== src
                            ? src
                            : null
                        )
                    )}
                />
            ))}
        </ul>
    );
};

When selectedImage is null it means that no image has been clicked. And on click, we're either setting it to the clicked image, or setting it back to null if the same image has been clicked. And then you'd apply the border to images with active class. Simplified CSS:

.my-image {
    border: 5px solid transparent;
}

.my-image.active {
    border-color: red;
}
about 4 years ago · Juan Pablo Isaza 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