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

147
Visualizações
How can I accumulate string values ​in useState as an array value in React?

I created a value called degrowth LengthImg with the useState array. When the deleteImgfunc function is executed, the value is entered as a string value. if deleteImgfunc excuted The first may contain a "buger" value, if deleteImgfunc excuted second time "pizza" value may come in.

At this time, I want to store string values ​​in degrowthLengthImg as an array.

like this expected answer

    degrowthLengthImg = ["buger", "pizza"]

so i made my code but it comes out like blow

   degrowthLengthImg = ["b", "g", ....]

this is my code

    const [degrowthLengthImg, setDegrowthLengthImg] = useState([])

    const deleteImgfunc = useCallback((value: string) => {
            setDegrowthLengthImg([...degrowthLengthImg, value]);

    }, []);

 return(
     <Pressable style={farmDiaryViewWrapperStyle.imgcloseBtn}
     onPress={() => 
    deleteImgfunc?.(v?.name)}
    >
      )

How can i fix my code?

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Use the following to add value to the array. Do not use the push function to push to an array. Also, the useCallback is unnecessary.

    const [degrowthLengthImg, setDegrowthLengthImg] = useState([])

    const deleteImgfunc = useCallback((value: string) => {
           // If you want to add to array
            setDegrowthLengthImg(deg => [...deg, value]);
    }, []);
about 4 years ago · Santiago Trujillo Relatório

0

You should use setDegrowthLengthImg as,

 setDegrowthLengthImg(degrowthLengthImg => [...degrowthLengthImg, value]);
about 4 years ago · Santiago Trujillo 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