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

267
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
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