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

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