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

71
Views
How do i prevent my checkbox input from firing multiple events when I select the checkbox?

I created a smaller version of my project where I wanted to demonstrate my problem with checkboxes. Basically, I want to remove the table row of the checkbox that has been checked.

  1. My first problem arises when I click my checkbox where it doesn't return true but false on the initial click when checking the box using const checked = e.target.checked. Therefore I used if(!checked) to get around this problem. How do I get the checkbox to input true when I first click the unchecked box?

  2. When I click on the checkbox, it'll push two ids of the same id into the array ids instead of one like this [123,123] how would I get around pushing only one?

function App() {
  let initialState = [
    {id: 123, text: "Hello world"},
    {id: 234, text: "Afternoon world"},
    {id: 345, text: "Evening world"}
  ];
  const [alerts, setAlerts] = useState(initialState);
  const [list, setList] = useState([]);
  // let ids = [];
  const handleRemove = () => {
    ids.forEach((id) => {
      setAlerts(alerts.filter((a) => a.id !== id))
    });
  };

  let ids = [];
  const addToList = (e, id) => {
    const checked = e.target.checked;
    if (!checked) {
     ids.push(id);
     console.log(ids);
    }
 };

  return (
    <div>
      <RuxTableBody>
        {alerts.map((alert) => {
          const { id, text } = alert;
          return (
            <RuxTableRow key={id}>
              <RuxCheckbox onClick={(e) => addToList(e, id)}>
                {text}
              </RuxCheckbox>
            </RuxTableRow>
          );
        })}
      </RuxTableBody>
      <RuxButton onClick={handleRemove}>Acknowledge</RuxButton>
    </div>
  );
}
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!