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

146
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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