Skip to content

Instantly share code, notes, and snippets.

View akullpp's full-sized avatar

aku akullpp

View GitHub Profile
@akullpp
akullpp / use-deep-effect.ts
Created February 8, 2024 23:49
Deep compare version of useEffect for React
import { useEffect, useMemo, useRef } from 'react'
import { isEqual } from 'lodash-es'
type UseEffectParams = Parameters<typeof useEffect>
type EffectCallback = UseEffectParams[0]
type DependencyList = UseEffectParams[1]
type UseEffectReturn = ReturnType<typeof useEffect>
const isPrimitive = (val: unknown) => {
@akullpp
akullpp / use-auto-save.ts
Created February 8, 2024 23:50
Autosave hook for react-hook-form
import { useCallback } from 'react'
import { debounce } from 'lodash-es'
import { useWatch } from 'react-hook-form-mui'
import type { useForm } from 'react-hook-form-mui'
import { useDeepEffect } from '@/hooks/use-deep-effect'
const useAutoSave = (context: ReturnType<typeof useForm>, onSubmit: (data: unknown) => void) => {
const watch = useWatch({ control: context.control })