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

108
Visualizações
React changing state of specific element on button click

I'm building a React component thats basically a dropdown of sorting options. When a user clicks on a sort selection, the UI will update in accordance to the user's selection. It looks like this:

enter image description here

I want to display the blue checkmark on the actively selected sort. I know that using states in React is the way to go but I'm having trouble binding the state to the specifically selected user sort.

This is my component SortSelection.jsx:

const SortSelection = ({
  options, extraClass, handleSort,
}) => {
  const [isActive, setActive] = React.useState(false);

  const handleClickSort = (e) => {
    const button = e.target.closest('button');
    if (button) {
      const sort = button.getAttribute('data-sort');
      if (button.classList.contains('selectionDropdown_menu--children-sort') && sort) {
        setActive(!isActive);
      }
    }
  };

  return (
    <div className={classList}>
      SORT BY: {currentSortDisplayName}
      <ul className="selectionDropdown_menu">
        {options.map((option, index) => <li key={index} value={option.value} className="selectionDropdown_menu--children">
           <Check width="20" height="20"/>
           <Button text={option.name} className={`selectionDropdown_menu--children-sort ${isActive ? 'active' : null}`} onClickFunc={handleClickSort.bind(this)} data-sort={option.value} data-name={option.name}/>
          </li>)}
      </ul>
    </div>
  );
};

Unfortunately, when I update the state with setActive hook, both components get the 'active' class instead of the actual sort that the user clicked on. My idea is to show/hide the blue checkmarks(Check component) with CSS by checking the existence of the 'active' class.

How can I fix this to grab only the user's actual selected sort, in order to display the blue checkmark? I also need to ensure that on page load, the default sort of Newest will be shown, so I have to show the correct blue checkmark on page load.

about 4 years ago · Juan Pablo Isaza
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