UNPKG

1.55 kBSource Map (JSON)View Raw
1{"version":3,"file":"useReactiveVar.js","sourceRoot":"","sources":["../../../src/react/hooks/useReactiveVar.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAG5C,MAAM,UAAU,cAAc,CAAI,EAAkB;IAClD,IAAM,KAAK,GAAG,EAAE,EAAE,CAAC;IAInB,IAAM,QAAQ,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IAKpC,SAAS,CAAC;QACR,IAAM,iBAAiB,GAAG,EAAE,EAAE,CAAC;QAC/B,IAAI,KAAK,KAAK,iBAAiB,EAAE;YAG/B,QAAQ,CAAC,iBAAiB,CAAC,CAAC;SAC7B;aAAM;YACL,OAAO,EAAE,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC;SAClC;IACH,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IAEZ,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import { useEffect, useState } from 'react';\nimport { ReactiveVar } from '../../core';\n\nexport function useReactiveVar<T>(rv: ReactiveVar<T>): T {\n const value = rv();\n\n // We don't actually care what useState thinks the value of the variable\n // is, so we take only the update function from the returned array.\n const setValue = useState(value)[1];\n\n // We subscribe to variable updates on initial mount and when the value has\n // changed. This avoids a subtle bug in React.StrictMode where multiple\n // listeners are added, leading to inconsistent updates.\n useEffect(() => {\n const probablySameValue = rv();\n if (value !== probablySameValue) {\n // If the value of rv has already changed, we don't need to listen for the\n // next change, because we can report this change immediately.\n setValue(probablySameValue);\n } else {\n return rv.onNextChange(setValue);\n }\n }, [value]);\n\n return value;\n}\n"]}
\No newline at end of file