refun
Version:
A collection of React Hook-enabled functions that compose harmoniously with each other. Similar to `recompose`, but:
1 lines • 1.24 kB
Source Map (JSON)
{"version":3,"sources":["on-change.ts"],"names":["useRef","EMPTY_OBJECT","shallowEqualByKeys","onChange","getFn","watchKeys","props","prevPropsRef","current"],"mappings":"AAAA,SAASA,MAAT,QAAuB,OAAvB;AACA,SAASC,YAAT,QAA6B,MAA7B;AACA,SAASC,kBAAT,QAAmC,SAAnC;AAEA,OAAO,IAAMC,QAAQ,GAAG,SAAXA,QAAW,CAAeC,KAAf,EAA0DC,SAA1D;AAAA,SAAqF,UAACC,KAAD,EAAiB;AAC5H,QAAMC,YAAY,GAAGP,MAAM,CAAIC,YAAJ,CAA3B;;AAEA,QAAIM,YAAY,CAACC,OAAb,KAAyBP,YAAzB,IAAyC,CAACC,kBAAkB,CAACK,YAAY,CAACC,OAAd,EAAuBF,KAAvB,EAA8BD,SAA9B,CAAhE,EAA0G;AACxG;AACAD,MAAAA,KAAK,CAACE,KAAD,CAAL;AACD;;AAEDC,IAAAA,YAAY,CAACC,OAAb,GAAuBF,KAAvB;AAEA,WAAOA,KAAP;AACD,GAXuB;AAAA,CAAjB","sourcesContent":["import { useRef } from 'react'\nimport { EMPTY_OBJECT } from 'tsfn'\nimport { shallowEqualByKeys } from './utils'\n\nexport const onChange = <P extends {}>(getFn: (props: P) => Promise<void> | void, watchKeys: (keyof P)[]) => (props: P): P => {\n const prevPropsRef = useRef<P>(EMPTY_OBJECT)\n\n if (prevPropsRef.current === EMPTY_OBJECT || !shallowEqualByKeys(prevPropsRef.current, props, watchKeys)) {\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n getFn(props)\n }\n\n prevPropsRef.current = props\n\n return props\n}\n"],"file":"on-change.js"}