Похожие чаты

Hello! I have a notification thingy that is removed automatically in

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);
};

6 ответов

24 просмотра
Emre- Автор вопроса

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.

Emre- Автор вопроса
Pedro Aguiar
Unless we can reproduce the error on our end, it's...

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

Emre
I don't have an error, it works as I want, I am as...

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

Emre- Автор вопроса
david
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

Похожие вопросы

Обсуждают сегодня

30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
Also, why can’t the community have a vote/ say when it comes to initiatives like buybacks. Isn’t the point of crypto decentralisation? Don’t we deserve input as long term supp...
👨🏽‍🦰
13
Чёт не понял, я ж правильной функцией воспользовался чтобы вывести отладочную информацию? но что-то она не ловится
notme
18
Привет)) уже кажется эту тему перемусолили, но вот я так и не понял. Я сейчас сижу на 27дюймов 2к мониторе. На Актуальной макоси, если я куплю 27д 4к монитор: - будет ли изобр...
Vladislav Piskunov
15
any reference of this implementation?
BitBuddha
29
Hi guys, any problem with Pulsebrige? Trying to transfer from wETH to ETH. First it tells me to connect my metamask "through mobile app" not desktop. Then I did and confirmed ...
Snowflakecrypto
13
У меня есть функция где происходит это: write_bit(buffer, 1); write_bit(buffer, 0); write_bit(buffer, 1); write_bit(buffer, 1); write_bit(buffer, 1); w...
~
13
Страшнейшая правда про списки ЦБ. С первых дней жизни P2P сферы, молодые человеки, начитавшись законодательной базы и "внутренних" документов, решили, что им противостоит сер...
Foxcool
3
Карта сайта