@mantine/hooks
Version:
A collection of 50+ hooks for state and UI management
1 lines • 1.75 kB
Source Map (JSON)
{"version":3,"file":"use-shallow-effect.cjs","names":["shallowEqual"],"sources":["../../src/use-shallow-effect/use-shallow-effect.ts"],"sourcesContent":["import { useEffect, useRef } from 'react';\nimport { shallowEqual } from '../utils/shallow-equal/shallow-equal';\n\nfunction shallowCompare(prevValue?: React.DependencyList | null, currValue?: React.DependencyList) {\n if (!prevValue || !currValue) {\n return false;\n }\n\n if (prevValue === currValue) {\n return true;\n }\n\n if (prevValue.length !== currValue.length) {\n return false;\n }\n\n for (let i = 0; i < prevValue.length; i += 1) {\n if (!shallowEqual(prevValue[i], currValue[i])) {\n return false;\n }\n }\n\n return true;\n}\n\nfunction useShallowCompare(dependencies?: React.DependencyList) {\n const ref = useRef<React.DependencyList | null | undefined>([]);\n const updateRef = useRef<number>(0);\n\n if (!shallowCompare(ref.current, dependencies)) {\n ref.current = dependencies;\n updateRef.current += 1;\n }\n\n return [updateRef.current];\n}\n\nexport function useShallowEffect(cb: () => void, dependencies?: React.DependencyList): void {\n useEffect(cb, useShallowCompare(dependencies));\n}\n"],"mappings":";;;;AAGA,SAAS,eAAe,WAAyC,WAAkC;AACjG,KAAI,CAAC,aAAa,CAAC,UACjB,QAAO;AAGT,KAAI,cAAc,UAChB,QAAO;AAGT,KAAI,UAAU,WAAW,UAAU,OACjC,QAAO;AAGT,MAAK,IAAI,IAAI,GAAG,IAAI,UAAU,QAAQ,KAAK,EACzC,KAAI,CAACA,sBAAAA,aAAa,UAAU,IAAI,UAAU,GAAG,CAC3C,QAAO;AAIX,QAAO;;AAGT,SAAS,kBAAkB,cAAqC;CAC9D,MAAM,OAAA,GAAA,MAAA,QAAsD,EAAE,CAAC;CAC/D,MAAM,aAAA,GAAA,MAAA,QAA2B,EAAE;AAEnC,KAAI,CAAC,eAAe,IAAI,SAAS,aAAa,EAAE;AAC9C,MAAI,UAAU;AACd,YAAU,WAAW;;AAGvB,QAAO,CAAC,UAAU,QAAQ;;AAG5B,SAAgB,iBAAiB,IAAgB,cAA2C;AAC1F,EAAA,GAAA,MAAA,WAAU,IAAI,kBAAkB,aAAa,CAAC"}