Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

156
Vistas
I made a clock class in JavaScript, but it's not working properly when imported. I can only get the seconds and the interval doesn't seem to work too
import React, { Component } from 'react';
import { Text, View } from 'react-native';


export default class Clock extends Component {

  componentDidMount(){

    setInterval(() => (

      this.setState(
        { curHours :  new Date().getHours()}
      ),
      this.setState(
        { curMins :  new Date().getMinutes()}
      ),
      this.setState(
        { curSeconds :  new Date().getSeconds()}
      )
    ), 1000);
  }


state = {curHours:new Date().getHours()};
state = {curMins:new Date().getMinutes()};
state = {curSeconds:new Date().getSeconds()};

  renderHours() {
    return (
  <Text>{'Hours:'}{this.state.curHours}</Text>
    );
  }
  renderMinutes() {
    return (
  <Text>{'Minutes:'}{this.state.curMinutes}</Text>
    );
  }
  renderSeconds() {
    return (
  <Text>{'Seconds:'}{this.state.curSeconds}</Text>
    );
  }
}

-I'm trying to make an app that can keep track of time kinda like a daily planner. So I need to get the current time in real time during app run time. The app is supposed to tell the user that they have failed to accomplish a certain task in a given time for example. I tried exporting the clock.js and using its functions but only the renderSeconds() is working, the others are only showing blanks.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

When you're defining state three times, only the last one is persisted because you're overwriting the previous variable. In addition, your initial state should be declared in a constructor. Add this to the top of your class

constructor() {
  this.state = {
    curHours:new Date().getHours(),
    curMins:new Date().getMinutes(),
    curSeconds:new Date().getSeconds(),
  }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

I think functional components will be much simpler to solve this, but it's just my opinion. Here's a link to an example

about 4 years ago · Juan Pablo Isaza Denunciar
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 Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda