FRONTEND_1 Telegram 3981
🔥 Как дебаунсить useEffect без лишних библиотек

Когда useEffect триггерится слишком часто — например, при вводе в input — можно легко добавить дебаунс. Без lodash, только setTimeout.


import { useEffect } from 'react'

function useDebouncedEffect(effect: () => void, deps: any[], delay: number) {
useEffect(() => {
const handler = setTimeout(() => effect(), delay)

return () => clearTimeout(handler)
}, [...deps, delay])
}


📌 Использование:


useDebouncedEffect(() => {
fetch(`/api/search?q=${query}`)
}, [query], 500)


Плюсы:
– Простой и читаемый хук
– Не надо тащить lodash
– Управляемый debounce прямо в useEffect

⚠️ Важно: deps должны быть стабильны. Заворачивай колбэки в useCallback при необходимости.

👉 @frontend_1
👍6👎21🤔1🤣1



tgoop.com/frontend_1/3981
Create:
Last Update:

🔥 Как дебаунсить useEffect без лишних библиотек

Когда useEffect триггерится слишком часто — например, при вводе в input — можно легко добавить дебаунс. Без lodash, только setTimeout.


import { useEffect } from 'react'

function useDebouncedEffect(effect: () => void, deps: any[], delay: number) {
useEffect(() => {
const handler = setTimeout(() => effect(), delay)

return () => clearTimeout(handler)
}, [...deps, delay])
}


📌 Использование:


useDebouncedEffect(() => {
fetch(`/api/search?q=${query}`)
}, [query], 500)


Плюсы:
– Простой и читаемый хук
– Не надо тащить lodash
– Управляемый debounce прямо в useEffect

⚠️ Важно: deps должны быть стабильны. Заворачивай колбэки в useCallback при необходимости.

👉 @frontend_1

BY Frontend разработчик




Share with your friend now:
tgoop.com/frontend_1/3981

View MORE
Open in Telegram


Telegram News

Date: |

As the broader market downturn continues, yelling online has become the crypto trader’s latest coping mechanism after the rise of Goblintown Ethereum NFTs at the end of May and beginning of June, where holders made incoherent groaning sounds and role-played as urine-loving goblin creatures in late-night Twitter Spaces. During a meeting with the president of the Supreme Electoral Court (TSE) on June 6, Telegram's Vice President Ilya Perekopsky announced the initiatives. According to the executive, Brazil is the first country in the world where Telegram is introducing the features, which could be expanded to other countries facing threats to democracy through the dissemination of false content. Choose quality over quantity. Remember that one high-quality post is better than five short publications of questionable value. Telegram Android app: Open the chats list, click the menu icon and select “New Channel.” With the “Bear Market Screaming Therapy Group,” we’ve now transcended language.
from us


Telegram Frontend разработчик
FROM American