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

113
Vistas
Update timestamp text after a given time in React

I'm building a component for notifications. It looks like this:

const renderTime = eventTime => {
  let toDisplay = eventTime.toLocaleDateString();
  if (isJustNow(eventTime)) {
    toDisplay = "Just now";
  } else if (isToday(eventTime))
    toDisplay = eventTime.toLocaleTimeString("en", {
      hour: "2-digit",
      minute: "2-digit"
    });

  return <Text>{toDisplay}</Text>;
};

export default function NotificationCard({ item }) {
  return (
      <ListItem accessoryRight={() => renderTime(item.created)} />  
  );
}

This is React Native, but it shouldn't matter.

What I want is, if the notification is from a minute ago to display Just now. If it is from more than a minute ago to display to full time. It is working okay, but it stuck on Just now.

My question is: How do I make the renderTime function reevaluate the text to display after a minute?

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

0

It depends on how you are using this component. If the component is a dumb component, You need to watch prop time on render and set time local timebases on that. Or you can create a smart component with setInterval to update time. Please check out below draft version of the code.

// Smart Component

const isJustNow = (time) => Date.now() - time < 5000;
const isToday = (time) => Date.now() - time < 24 * 60 * 1000;
const Timer = ({ time: currentTime }) => {
  const [time, setTime] = useState(currentTime);
  useEffect(() => {
    const id = setInterval(() => {
      setTime(new Date(time.getTime() - 1000));
    }, 1000);
    return () => clearInterval(id);
  }, [currentTime, time]);
  let toDisplay = time.toLocaleDateString();
  if (isJustNow(time)) {
    toDisplay = "Just now";
  } else if (isToday(time))
    toDisplay = time.toLocaleTimeString("en", {
      hour: "2-digit",
      minute: "2-digit"
    });

  return <h2>{toDisplay}</h2>;
};
export default function App() {
  const times = [new Date(), new Date(Date.now() - 2000)];
  return (
    <div className="App">
      {times.map((time) => (
        <Timer key={time} time={time} />
      ))}
    </div>
  );
}

Here in the above example, You are using currentTime as the initial time when it is mounted. Once the component is mounted, It will run its own scheduler to update time in the local state.

Working code:

(codesandbox)(https://codesandbox.io/embed/optimistic-leaf-y24nz?fontsize=14&hidenavigation=1&theme=dark)

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