{"version":3,"sources":["../../src/use-latest/index.ts"],"sourcesContent":["import { useRef } from 'react'\n\nimport type { MutableRefObject } from 'react'\n\n/**\n * A React Hook that returns a ref to the latest value. It can be useful to prevent **stale state** in timer callback or async context.\n *\n * This React Hook writing `ref.current` during the render, which is not officially recommended, but is needed to avoid closure issues and it is predictable,\n *\n * provided that the useLatest value is not directly involved in rendering, it should ONLY be consumed during the effect phase.\n *\n * @see https://react.dev/reference/react/useRef#avoiding-recreating-the-ref-contents\n *\n * @see https://github.com/alibaba/hooks/issues/2495#issuecomment-1988915570\n */\nexport function useLatest<T>(value: T): MutableRefObject<T> {\n  const latestRef = useRef<T>(value)\n\n  // Prevent frequent updates when the value has not actually changed\n  if (!Object.is(latestRef.current, value)) {\n    latestRef.current = value\n  }\n\n  return latestRef\n}\n"],"names":["useLatest","value","latestRef","useRef","Object","is","current"],"mappings":";;;;+BAegBA;;;eAAAA;;;qBAfO;AAehB,SAASA,UAAaC,KAAQ;IACnC,IAAMC,YAAYC,IAAAA,aAAM,EAAIF;IAE5B,mEAAmE;IACnE,IAAI,CAACG,OAAOC,EAAE,CAACH,UAAUI,OAAO,EAAEL,QAAQ;QACxCC,UAAUI,OAAO,GAAGL;IACtB;IAEA,OAAOC;AACT"}