{"version":3,"sources":["../../src/useStateIfMounted/useStateIfMounted.ts"],"names":["useState","useIsComponentMounted","useStateIfMounted","initialValue","isComponentMounted","state","setState","value","useStateIfMounted_default"],"mappings":"8BAAA,OAAS,YAAAA,MAAoD,QAC7D,OAAOC,MAA2B,iDAclC,MAAMC,EACJC,GACsC,CACtC,MAAMC,EAAqBH,EAAsB,EAE3C,CAACI,EAAOC,CAAQ,EAAIN,EAAYG,CAAiB,EAQvD,MAAO,CAACE,EANyCE,GAAS,CACpDH,EAAmB,SACrBE,EAASC,CAAK,CAElB,CAE0B,CAC5B,EAEA,IAAOC,EAAQN","sourcesContent":["import { useState, type Dispatch, type SetStateAction } from 'react';\nimport useIsComponentMounted from '../useIsComponentMounted/useIsComponentMounted';\nimport type { UseStateIfMountedParamResponse, UseStateIfMountedParams } from './types';\n\n/**\n * Like React's [useState](https://reactjs.org/docs/hooks-reference.html#usestate)\n * but it makes sure the component that uses this hook is mounted when updating state\n *\n * @param {any} initialValue\n * @returns {[any, Diapatch<any>]} an array of 2 items\n *\n * the first is the current state, the second is a function that enables\n * updating the state if the component is not mounted\n */\n\nconst useStateIfMounted = <S>(\n  initialValue?: UseStateIfMountedParams<S>\n): UseStateIfMountedParamResponse<S> => {\n  const isComponentMounted = useIsComponentMounted();\n\n  const [state, setState] = useState<S>(initialValue as S);\n\n  const newSetState: Dispatch<SetStateAction<S>> = value => {\n    if (isComponentMounted.current) {\n      setState(value);\n    }\n  };\n\n  return [state, newSetState];\n};\n\nexport default useStateIfMounted;\n"]}