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

148
Visualizações
Hooks dont update the states correctly

Hi im trying to setting states in react but they dont update correctly, i have:

const [campusName, setCampusName] = useState("");
const [assists, setAssists] = useState({
    name:"",
    hour: "",
    day: "",
    month: "",
    year: "",
    studentid: "",
});

const [date, setDate] = useState({
    day: "",
    month: "",
    year: "",
    hour: "",
});

useEffect(()=>{
getData();
}, [])

const getData = async() =>{

    //campus name
    const campusResponse = await fetch("http://localhost:2000/info/campus/"+params.campusid);
    const campusData = await campusResponse.json();
    setCampusName(campusData.name);

    //date
    const date = new Date();   

    setDate({
        day: date.getDate(),
    month: date.getMonth()+1,
    year: date.getFullYear(),
    hour: `${date.getHours()}:${date.getMinutes()}`,
    });

    settingAssistence();
}

const settingAssistence = () => {
    setAsists({
        name: campusName,
        campusid: params.campusid,
        hour: date.hour,
        day: date.day,
        month: date.month,
        year: date.year,
        studentid: params.studentid,
    })

    console.log("result", asissts);
}

the console.log prints the assists object empty, but if i refresh the page 3 times it works, how can i set the states correctly? the fetch with http://localhost:2000/info/campus/ also works good and give me the correct data, but when i set it into the hook is not updating correctly.

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

0

Just add a small button to your UI to log the assists state. The Hooks take their time to execute and if you log your result right after setting the state is doesnt show the actual value.

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