5 seconds, but if a new notification is triggered, I want this 5 seconds to start anew. I know how to do this in vanilla JS, but in React, for some reason, I had to use useRef(), for the name assigned to the timeOut not get lost between renders.
Is this how it should be done?
let timeoutID = useRef();
...
const sendMessage = (message, messageType, timeout = 5000) => {
clearTimeout(timeoutID.current);
...
timeoutID.current = setTimeout(() => dispatch(removeNotification()), timeout);
};
Hello, a reminder for my simple react question
Unless we can reproduce the error on our end, it's a deadend. Set it in an online IDE such as Code Sandbox or repl.it to increase your chances, especially because the large majority of us is accessing Telegram from a mobile device.
I don't have an error, it works as I want, I am asking whether using useRef here makes sense. But will provide the tools, thanks for your time
there's no "right" way in programming, it all depends on so many variables does it work? is it maintainable? is it testable? easy to change? scalable? for a throwaway project, as long as it works? for a project involving many devs, maybe testability is more important?
Fwiw you should not need a ref for it
In the case of a state change I was losing the timeout ID so was not able to reset the timeout. This for sure did not feel natural, and probably was a result of a huuuge component that had many states in it
Обсуждают сегодня