refun
Version:
A collection of React Hook-enabled functions that compose harmoniously with each other. Similar to `recompose`, but:
1 lines • 1.34 kB
Source Map (JSON)
{"version":3,"sources":["map-with-props-memo.ts"],"names":["mapWithPropsMemo","getFn","watchKeys","props","prevProps","EMPTY_OBJECT","prevResult","current"],"mappings":";;;;;;;;;AAAA;;AACA;;AAEA;;AAEO,IAAMA,gBAAgB,GAAG,SAAnBA,gBAAmB,CAA6BC,KAA7B,EAAqDC,SAArD;AAAA,SAAgF,UAACC,KAAD,EAA6B;AAC3I,QAAMC,SAAS,GAAG,mBAAUC,kBAAV,CAAlB;AACA,QAAMC,UAAU,GAAG,mBAAUD,kBAAV,CAAnB;;AAEA,QAAIC,UAAU,CAACC,OAAX,KAAuBF,kBAAvB,IAAuC,CAAC,+BAAmBD,SAAS,CAACG,OAA7B,EAAsCJ,KAAtC,EAA6CD,SAA7C,CAA5C,EAAqG;AACnGE,MAAAA,SAAS,CAACG,OAAV,GAAoBJ,KAApB;AACAG,MAAAA,UAAU,CAACC,OAAX,GAAqBN,KAAK,CAACE,KAAD,CAA1B;AACD;;AAED,sCACKA,KADL,EAEKG,UAAU,CAACC,OAFhB;AAID,GAb+B;AAAA,CAAzB","sourcesContent":["import { useRef } from 'react'\nimport { EMPTY_OBJECT } from 'tsfn'\nimport type { TExtend } from 'tsfn'\nimport { shallowEqualByKeys } from './utils'\n\nexport const mapWithPropsMemo = <P extends {}, R extends {}>(getFn: (props: P) => R, watchKeys: (keyof P)[]) => (props: P): TExtend<P, R> => {\n const prevProps = useRef<P>(EMPTY_OBJECT)\n const prevResult = useRef<R>(EMPTY_OBJECT)\n\n if (prevResult.current === EMPTY_OBJECT || !shallowEqualByKeys(prevProps.current, props, watchKeys)) {\n prevProps.current = props\n prevResult.current = getFn(props)\n }\n\n return {\n ...props,\n ...prevResult.current,\n }\n}\n"],"file":"map-with-props-memo.js"}