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

88
Visualizações
React - Can we calculate the time between onMouseEnter and onMouseLeave over a component?

I'm trying to build a React component where I need to calculate the time difference between onMouseEnter and onMouseLeave when I hover over a div? I have added "time" in the state but dunno how to continue it. Since I'm a beginner in React I'm not able to get it working. Please help....Also, when I try to display these elements, I'm not sure about the timeStamp property that gets displayed.

import React from 'react';
import '../Hover/Hover.css';


class Hover extends React.Component {
    constructor(props) {
        super(props);
        this.handleMouseEnter = this.handleMouseEnter.bind(this);
        this.handleMouseLeave = this.handleMouseLeave.bind(this);
        this.state = {
            isHovering: false,
            time: 0
        }
    }

    handleMouseEnter(elementEnter){
        console.log("mouseenter : ", elementEnter.timeStamp)
        this.setState({
            isHovering: true
        })
    }

    handleMouseLeave(elementLeave) {
        console.log("mouseleave : ", elementLeave.timeStamp)
        this.setState({
            isHovering: false
        })
    }

    render() {
        return(
            <div className="hover" onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave}>
                <h1>Hover Component</h1>
            </div>
            
        )
    }
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

import React from 'react';
import '../Hover/Hover.css';


class Hover extends React.Component {
    constructor(props) {
        super(props);
        this.handleMouseEnter = this.handleMouseEnter.bind(this);
        this.handleMouseLeave = this.handleMouseLeave.bind(this);
        this.state = {
            isHovering: false,
            enterTime: 0,
            leavingTime: 0,
        }
    }

    handleMouseEnter(elementEnter){
        this.setState({
            isHovering: true, leavingTime: Date.now(),
        })
    }

    handleMouseLeave(elementLeave) {
        this.setState({
            isHovering: false, enterTime: Date.now(),
        })
    }

    render() {
        const hoverTime = this.sate.leavingTime - this.state.enterTime;
        
        return(
            <div className="hover" onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave}>
                <h1>Hover Component</h1>
                { hoverTime > 0 && 'duration is:' + hoverTime }
            </div>
            
        )
    }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You can add a state to store time handleMouseEnter.

constructor(props) {
    super(props);
    this.handleMouseEnter = this.handleMouseEnter.bind(this);
    this.handleMouseLeave = this.handleMouseLeave.bind(this);
    this.state = {
        isHovering: false,
        time: 0,
        start: 0
    }
}

handleMouseEnter(elementEnter){
    console.log("mouseenter : ", elementEnter.timeStamp)
    this.setState({
        isHovering: true,
        start: new Date()
    })
}

handleMouseLeave(elementLeave) {
    const newTime = new Date();
    this.setState({
        isHovering: false,
        time: Math.abs(new Date()-this.state.start) // milisecond
        start: 0
    })
}
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