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

123
Visualizações
useEffect value remains same after defining it in useEffect in ReactJS

I have a component in which I want to fetch an API after rendering, and put a response data in useState value.

So, after fetching API I'm trying to setState in useEffect callback, but nothing works.

const [values, setValues] = useState([])

useEffect(() => {
    fetchValues().then(function(result) {
        setValues(result.data)
    })
}, []);

Thanks for your help in advance.

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

0

You need to chain another then method. For instance, if the returned data is JSON, to convert it to JS object:

const [values, setValues] = useState([])

useEffect(() => {
    fetchValues().then(result => result.json())
   .then(data => {
       setValues(data)
    })
}, []);

If the returned data is text, replace result.json() with result.text()

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