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

269
Views
How do I make onPress remember it was pressed?

How do I make pressedItem be remembered and not changed on next onPress, to make it deselect if only the function is called again? Perhaps the ids to be remembered in an array and the array alters its length between pressedItem and dePressedItem?

  const [pressedItem, setPressedItem] = useState(null);
let selected = [];
  const changeColor = (itemid) => {
    setPressedItem(itemid);
}

  };
  <FlatGrid data={interesi}
    style={tw``}
    spacing={10}
        renderItem={({ item }) => (
      <TouchableWithoutFeedback onPress={() => changeColor(item.id)}>
      <View style={ pressedItem === item.id ? item.firstStyle : item.secondStyle }>
        <Text style={tw`text-black`}>{item.english}</Text>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

If no other ID can be pressed you can use:

const changeColor = (itemid) => {
    if (!pressedItem) {
        setPressedItem(itemid);
    }
};

If you are looking to not press the same id as before then:

const changeColor = (itemid) => {
    if (pressedItem !== itemid) {
        setPressedItem(itemid);
    }
};

EDIT:

Updating the answer based on comments. Then you would need to do something like holding a map of the pressed state of the itemid:

const [pressedItems, setPressedItems] = useState({});
let selected = [];

const changeColor = (itemid) => {
    pressedItems[itemid] = !pressedItems[itemid];
    setPressedItem(pressedItems);
};
about 4 years ago · Juan Pablo Isaza Report

0

You should add the id of your item to an array as you said:

const [pressedIds, setPressedIds] = useState<number[]>([]);

const isMyIdPressed = (id: number) => {
  if (pressedIds.include(id)) {
    setPressedIds(pressedIds.splice(pressedIds.indexOf(id), 1));
  } else {
    setPressedIds([...pressedIds, id]);
  }
}

<FlatGrid data={interesi}
    style={tw``}
    spacing={10}
        renderItem={({ item }) => (
      <TouchableWithoutFeedback onPress={() => isMyIdPressed(item.id)}>
      <View style={pressedIds.include(item.id) ? item.firstStyle : item.secondStyle }>
        <Text style={tw`text-black`}>{item.english}</Text>

Whenever you press an id, just check if it is in the array, if yes add the id to the array and if no remove it from the array.

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!