Skip to content

Instantly share code, notes, and snippets.

View Alimjanov-Ibragim's full-sized avatar
🎯
Focusing

Ibragim Alimjanov-Ibragim

🎯
Focusing
View GitHub Profile
@Alimjanov-Ibragim
Alimjanov-Ibragim / useDebounce.js
Created March 27, 2023 08:49 — forked from ali-sabry/useDebounce.js
This hook will allow me to debounce any function that I want to run after a certain delay. For example, if I have an input field that triggers a search function on every keystroke, this hook will prevent unnecessary API calls by delaying the search function until the user stops typing for a certain period.
import { useState, useEffect } from 'react';
export const useDebounce = (value, delay) => {
const [debouncedValue, setDebouncedValue] = useState(value);
useEffect(() => {
const timer = setTimeout(() => {
setDebouncedValue(value);
}, delay);
@Alimjanov-Ibragim
Alimjanov-Ibragim / day_hours.json
Created May 17, 2023 07:22 — forked from wching/day_hours.json
JSON file with all hours in the day
[
{
"military_format": "0000",
"twenty_four_hour_format": "00:00",
"standard_format": "00:00",
"time_of_day":"md"
},
{
"military_format": "0030",
"twenty_four_hour_format": "00:30",
import { useEffect, useRef, useState } from 'react'
function useThrottle<T>(value: T, interval = 500): T {
const [throttledValue, setThrottledValue] = useState<T>(value)
const lastExecuted = useRef<number>(Date.now())
useEffect(() => {
if (Date.now() >= lastExecuted.current + interval) {
lastExecuted.current = Date.now()
setThrottledValue(value)
@Alimjanov-Ibragim
Alimjanov-Ibragim / react-use-throttle-hook-usage.tsx
Created September 5, 2023 08:19 — forked from loonywizard/react-use-throttle-hook-usage.tsx
Example of usage of React useThrottle hook
import React, { useEffect, useState } from 'react'
import { useThrottle } from './useThrottle'
export default function App() {
const [value, setValue] = useState('hello')
const throttledValue = useThrottle(value)
useEffect(() => console.log(`throttledValue changed: ${throttledValue}`), [
throttledValue,