0

I have a specific problem that is keeping me awake this whole week.

I have a parent component which has a pop-up children component. When I open the page the pop-up shows off and after 5 seconds it disappears with a setTimeout.

This pop-up has an input element in it.

I want the pop-up to disappear after 5 seconds or if I click to digit something in the input. I tried to create a timerRef to the setTimeout and closes it in the children but it didn't work.

Can you help me, please? Thanks in advance.

ParentComponent.tsx


const ParentComponent = () => {
    const [isVisible, setIsVisible] = useState(true)
    timerRef = useRef<ReturnType<typeof setTimeout>>()

    timerRef.current = setTimeout(() => {
        setIsVisible(false)
    }, 5000)

    useEffect(() => {
        return () => clearTimeout()
    })

    return (
        <div>
            <ChildrenComponent isVisible={isVisible} inputRef={timerRef} />
        </div>
    )
}

ChildrenComponent.tsx


const ChildrenComponent = ({ isVisible, inputRef}) => {
    return (
        <div className=`${isVisible ? 'display-block' : 'display-none'}`>
            <form>
                <input onClick={() => clearTimeout(inputRef.current as NodeJS.Timeout)} />
            </form>
        </div>
    )
}
2
  • To clarify, when they type in the input, do you want it to close immediately, or cancel the 5s timer completely?
    – adsy
    Jan 28 at 1:43
  • Because of the setTimeout the normal behavior of the pop-up is to disappear after 5 seconds. I need to stop this behavior if the user starts to type in the input. Jan 28 at 11:40

1 Answer 1

0

You're setting a new timer every time the the component re-renders, aka when the state changes which happens in the timeout itself.

timerRef.current = setTimeout(() => {
  setIsVisible(false);
}, 5000);

Instead you can put the initialization in a useEffect.

useEffect(() => {
  if (timerRef.current) return;

  timerRef.current = setTimeout(() => {
    setIsVisible(false);
  }, 5000);
  return () => clearTimeout(timerRef.current);
}, []);

You should also remove the "loose" useEffect that runs on every render, this one

useEffect(() => {
  return () => clearTimeout();
});
2
  • It doesn't work for me. I need to create the timeRef.current outside of useEffect because I need to pass it to the children component. If I create the timeRef inside useEffectI can't pass it. Jan 29 at 14:36
  • It does not matter if you add the timeout value to the timeRef.current inside the useEffect, you can still pass the timeRef.current to the children.
    – RubenSmn
    Jan 29 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.