• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

175
Vistas
Problem with React useState (Changing between new & old value of state)

I want to make app for display live app on big devices. I just create the function for checking time for every second (just for sim).

Time state work fine in display, but if I check the artist "right now" here is a problem. Every time when state is updated, displayed name is jumping between new and old state.

Where is the problem? Thanks...

import React, { useState } from "react";
import Artist from "../Artist";
import Database from "../Data.json"


const Engine = () => {


    const [data, setData] = useState(Database);

    const [hour, setHour] = useState(null);
    const [minute, setMinute] = useState(null);
    const [second, setSecond] = useState(null);


    const [nowArtist, setNowArtist] = useState('');


    // Update time every second //
    const updateTime = () => {
        // const h = new Date().getHours(); //
        const h = 16;
        const m = new Date().getMinutes();
        const s = new Date().getSeconds();

        setHour(h);
        setMinute(m);
        setSecond(s);
    }



    const updateArtis = () => {
        data.stage1.filter(el => {
            if(el.startHour === hour) {
                setNowArtist(el)
            }
        })
    }


    setInterval(() => {
        updateTime();
        updateArtis();
    }, 1000);




    return (

        <Artist name={nowArtist.name} />
        
    )

}



export default Engine;

about 3 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda