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

181
Visualizações
Change values of array using state React Native

Today i tried to change values inside a array using state. So i have a simple array like -

  const [data, setData] = React.useState([
    {
      label: "data 1",
    },
    {
      label: "data 2",
    },
  ]);

Also i dont know is it good idea to use array in state. I created a button and text component also.

<View style={{ justifyContent: "flex-start", top: 50 }}>
  <Button
    title={"test"}
    onPress={() => {
      data[0].label = "fdsfsd";
      setData(data);
    }}
  />
  <Text>{data[0].label}</Text>
</View>

So my idea is when i click button to change data[0].label. But unfortunately it is not change? Why?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Change your onPress to this

onPress={() => {
  let temp = [...data];
  temp[0].label = 'asdasdasdasd';
  setData(temp);
}}

Working Example here

Read More about State Updates, Immutable Approaches etc here - https://www.freecodecamp.org/news/handling-state-in-react-four-immutable-approaches-to-consider-d1f5c00249d5/

about 4 years ago · Juan Pablo Isaza Relatório

0

You can try this, it will work

<View style={{ justifyContent: "flex-start", top: 50 }}>
  <Button
    title={"test"}
    onPress={() => {
      data[0].label = "fdsfsd";
    }}
  />
  <Text>{data[0].label}</Text>
</View>
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