Похожие чаты

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 ответов

10 просмотров
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

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

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

я не магистр хаскеля, но разве не может лейзи тип конвертнуться в не-лейзи запросив вычисление содержимого прям при инициализации?
deadgnom32 λ madao
49
30 BCH videos in Hausa Language successfully delivered. In December 2023, I created a Flipstarter to produce Bitcoin Cash videos in the Hausa language for greater outreach an...
Munnir
5
читать файл максимально быстро? странный вопрос))
zamtmn
53
Интересно, а майкрософт 365 может запустится на линукс минт?
Лисицка
17
How to create an OS in C? what to study?
Linus
18
Компания Elif ищет менеджера проектов, который будет заниматься поиском и ведением новых проектов. Прежде чем приступить к работе, вам нужно пройти наш недельный курс, где вы ...
Elif
5
братва! где куличи брать?
Alöscha
25
Привет, кто может сделать юзербота с апи? Задачи: - создавать группы - создавать каналы - задавать для созданных каналов аватарку или эмоджи, имя группы - добавлять в группы...
Lencore
11
на сколько можно увеличить swap при 16Гб оперативы?
Хасан Ахмаев
11
тоесть, указав return eax, сгенерируется никому ненужная инструкция mov eax,eax ?
Aiwan \ (•◡•) / _bot
24
Карта сайта