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

253
Views
React Native toggle add/remove element in an array based off toggling a button

I have a TouchableOpacity that can be both pressed and unpressed.

Here is my useState variable:

const [isPressMonday, setIsPressMonday] = useState(false);

and here is my toggling function:

    setIsPressMonday(!isPressMonday);
    setDays(days => [...days, 'Monday']) 

  };

The thing is, when a user un-clicks the button, it should remove Monday from the array. This would be simple if I could just check if(!isPressMonday) and remove it from the array, but the default is false so that is almost always true

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You don't need to check with isPressMonday state instead check if 'Monday' present in days state. If yes, remove else, add.

//assuming 'days' is your array state name
setIsPressMonday(isPressMonday => !isPressMonday);
const index = days.indexOf('Monday');
if (index !== -1) {
  let tempDays = days
  tempDays.splice(index, 1);
  setDays(tempDays)
}
else {
  setDays(days => [...days, 'Monday'])
}
about 4 years ago · Juan Pablo Isaza Report
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!