Skip to content

Instantly share code, notes, and snippets.

View loonywizard's full-sized avatar
🍊

Vladimir Nikitin loonywizard

🍊
View GitHub Profile
@loonywizard
loonywizard / react-use-throttle-hook-usage.tsx
Last active September 5, 2023 08:19
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,
@loonywizard
loonywizard / react-use-throttle-hook.ts
Created March 30, 2021 14:14
React useThrottle hook
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)
@loonywizard
loonywizard / js-confetti-example.js
Last active March 15, 2021 17:22
JS-Confetti usage example
import JSConfetti from 'js-confetti'
const jsConfetti = new JSConfetti()
jsConfetti.addConfetti()
@loonywizard
loonywizard / git-branches-commands-list.sh
Last active February 25, 2020 19:55
List of commands for working with git branches
# get list of all branches, current one will be marked with the * character
git branch
# create new branch called branch_name
git branch branch_name
# switch to existing branch called branch_name
git checkout branch_name
# create and checkout to just created branch, called branch_name
@loonywizard
loonywizard / compareToObjects.js
Last active November 15, 2017 17:40
This javascript function compares two objects by specific field, that function is helpful when you a sorting array of objects
/*
* Usage:
* const sortedArray = arrayOfObjects.sort((a, b) => compareTwoObjectsByField(a, b, 'fieldName'));
*/
export function compareTwoObjectsByField(a, b, field) {
if (a[field] < b[field]) {
return -1;
} else if (a[field] > b[field]) {
return 1;
}