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

225
Vistas
animation with react and useRef hook? is there another way?

I want to make smth like clock with react.I hope to do it with help of ref, but it doesn't work sometimes. Is there another solution of this task, or what i do wrong?

import React, { useRef } from 'react';
import classes from "./Synchroniser.module.css";
const Synchroniser = () => {
    const point:any = useRef(null);
    var k = 0;
    if (point.current !== null) {
        setInterval(()=>{
                if(k<360){
                k++;
                console.log(k);
            }else{k=0}
            point.current.style.transform = `rotate(${k}deg)`;
            },100);
    }else{
        console.log(0)
    }
    return(
        <div>
            <h1 ref={point}>RLY?</h1>
        </div>
    );
};

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

0

Go to bottom for the pure CSS solution

React Solution

This solves your problem but the k variable adds twice as much as it needs to, I don't know why. For the k variable to persist between states and not reset the clock every rerender you have to use it with useRef too.

  const point = useRef(null);
  const k = useRef(0);
  useEffect(() => {

    if (point.current !== null) {
    setInterval(() => {
        if (k.current < 360) {
          k.current = k.current + 1;
        } else {
          k.current = 0;
        }
        point.current.style.transform = `rotate(${k.current}deg)`;
      }, 1000);
    } else {
      console.log("0");
    }
  },[]);

CSS Solution CodeSandbox Link

JSX

<div className="clock">
  <div className="spin"></div>
</div>

CSS

.clock {
  height: 250px;
  width: 250px;
  border-radius: 9999px;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #eeeeee;
}
.spin {
  width: 90%;
  height: 2px;
  position: relative;
  background: white;
  -webkit-animation:spin 60s linear infinite;
  -moz-animation:spin 60s linear infinite;
  animation:spin 60s linear infinite;
}

.spin::after,
.spin::before {
  height: 2px;
  content: " ";
  position: absolute;
  top: 0;
  width: 50%;
}
.spin::after {
  right: 0;
  background-color: black;
}
.spin::before {
  left: 0;
  background-color: #eeeeee;
}

@-moz-keyframes spin {
  100% {
    -moz-transform: rotate(360deg);
  }
}
@-webkit-keyframes spin {
  100% {
    -webkit-transform: rotate(360deg);
  }
}
@keyframes spin {
  100% {
    -webkit-transform: rotate(360deg);
    transform: rotate(360deg);
  }
}
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