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

303
Visualizações
How to remove event listener when a prop is false

When I lock the statement, the timer starts after 5s of mouse inactivity. But when the statement is unlock, I don't want that timer to keep running. However, in debug when the prop isLocked is false it enters in the else, but event doesn't seem to be removed, because it starts after 5s of mouse inactivity. I'm using react-timer-hook.

Do you have any suggestions? Here is my code

function LockSection(props: ILockSection, expiryTimestamp: any, autoStart: boolean) {

    autoStart = false;
    expiryTimestamp = new Date();
    expiryTimestamp.setSeconds(expiryTimestamp.getSeconds() + 75);

    const {
        seconds,
        minutes,
        start,
        restart,
    } = useTimer({ expiryTimestamp, onExpire: props.setLockedFunction, autoStart });

    const resetStartTimer = () => {
        const time = new Date();
        time.setSeconds(time.getSeconds() + 75);
        clearTimeout(0);
        restart(time, false);
        setTimeout(start, 5000);
    };

    useEffect(() => {
        if (props.isLocked) {
            window.addEventListener('mousemove', resetStartTimer);
        } else {
            window.removeEventListener('mousemove', resetStartTimer);
        }

        return () => {
            window.removeEventListener('mousemove', resetStartTimer);
        };

    }, [props.isLocked]);

    function lockStart() {
        props.setLockedFunction();
        
    }

    function unlockStop() {
        props.setLockedFunction();
        const time = new Date();
        time.setSeconds(time.getSeconds() + 75);
        restart(time, false);
    }

    return (
        <div className="lock-section">
           
            {!props.isLocked && props.userCanLock && <Button variant="info" onClick={lockStart}>Edit Statement</Button>}

            {props.isLocked && props.userCanLock && <div className="locked-by-user">
                <Button className="button-unlock" variant="info" onClick={unlockStop}>Unlock Statement</Button>
                <div className="form-control lockTimer">Timer&nbsp;{minutes < 10 && <span>0</span>}<span> {minutes}</span>: {seconds < 10 && <span>0</span>}<span>{seconds}</span></div> </div>}
            
            <TimerPopup triggerPopUp={minutes < 1} modalHeader={"Edit session is timing out"}
                modalBody={"Your changes will be lost."} />

            {!props.userCanLock && <div className="form-control locked">Statement already locked by {props.lockedByUser}</div>}
        </div>

    );
}

Thanks

about 4 years ago · Juan Pablo Isaza
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