UNPKG

@wordpress/compose

Version:
8 lines (7 loc) 1.62 kB
{ "version": 3, "sources": ["../../../src/hooks/use-warn-on-change/index.ts"], "sourcesContent": ["/**\n * Internal dependencies\n */\nimport usePrevious from '../use-previous';\n\n/**\n * Hook that performs a shallow comparison between the previous value of an object\n * and the new one, if there's a difference, it prints it to the console.\n * This is useful in performance related work, to check why a component re-renders.\n *\n * @example\n *\n * ```tsx\n * function MyComponent(props: Record<string, any>) {\n * useWarnOnChange(props);\n *\n * return \"Something\";\n * }\n * ```\n *\n * @param object Object which changes to compare.\n * @param prefix Just a prefix to show when console logging.\n */\nfunction useWarnOnChange(\n\tobject: Record< string, any > | any[],\n\tprefix: string = 'Change detection'\n): void {\n\tconst previousValues = usePrevious( object );\n\n\tObject.entries( previousValues ?? [] ).forEach( ( [ key, value ] ) => {\n\t\tif ( value !== object[ key as keyof typeof object ] ) {\n\t\t\t// eslint-disable-next-line no-console\n\t\t\tconsole.warn(\n\t\t\t\t`${ prefix }: ${ key } key changed:`,\n\t\t\t\tvalue,\n\t\t\t\tobject[ key as keyof typeof object ]\n\t\t\t);\n\t\t}\n\t} );\n}\n\nexport default useWarnOnChange;\n"], "mappings": ";AAGA,OAAO,iBAAiB;AAoBxB,SAAS,gBACR,QACA,SAAiB,oBACV;AACP,QAAM,iBAAiB,YAAa,MAAO;AAE3C,SAAO,QAAS,kBAAkB,CAAC,CAAE,EAAE,QAAS,CAAE,CAAE,KAAK,KAAM,MAAO;AACrE,QAAK,UAAU,OAAQ,GAA2B,GAAI;AAErD,cAAQ;AAAA,QACP,GAAI,MAAO,KAAM,GAAI;AAAA,QACrB;AAAA,QACA,OAAQ,GAA2B;AAAA,MACpC;AAAA,IACD;AAAA,EACD,CAAE;AACH;AAEA,IAAO,6BAAQ;", "names": [] }