{"name":"use-callback-ref","version":"1.2.0","description":"The same useRef, but with callback","main":"dist/es5/index.js","scripts":{"test":"ts-react-toolbox test","bootstrap":"ts-react-toolbox init","dev":"ts-react-toolbox dev","test:ci":"ts-react-toolbox test --runInBand --coverage","build":"ts-react-toolbox build","prepublish":"yarn build","release":"ts-react-toolbox release","lint":"ts-react-toolbox lint","static":"ts-react-toolbox publish","format":"ts-react-toolbox format","analyze":"ts-react-toolbox analyze"},"repository":{"type":"git","url":"git+https://github.com/theKashey/kashe.git"},"author":{"name":"theKashey","email":"thekashey@gmail.com"},"license":"MIT","devDependencies":{"ts-react-toolbox":"^0.2.2"},"engines":{"node":">=8.5.0"},"peerDependencies":{"react":"^16.8.0"},"jsnext:main":"dist/es2015/index.js","module":"dist/es2015/index.js","types":"dist/es5/index.d.ts","keywords":["react","hoot","useRef","createRef"],"size-limit":[{"path":"dist/es2015/index.js","limit":"0.5 KB"},{"path":"dist/es2015/useRef.js","limit":"0.5 KB"}],"gitHead":"88632357cea101370ba0ccb3f1ea45f02c12b886","bugs":{"url":"https://github.com/theKashey/kashe/issues"},"homepage":"https://github.com/theKashey/kashe#readme","_id":"use-callback-ref@1.2.0","_nodeVersion":"10.15.3","_npmVersion":"6.10.2","dist":{"integrity":"sha512-8UYRIng8806/LnaukZ468DdxGJkrnmKiGgexlLOUcgPp9Kcm5CxdItIymhdGXq8Hq47ItjOJ++JJKUHrId9fRw==","shasum":"e085a60851a9b5395fba9c7292507814643aa4f7","tarball":"http://registry-npm.ywwl.com/use-callback-ref/download/use-callback-ref-1.2.0.tgz","fileCount":42,"unpackedSize":18586,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdoX3ACRA9TVsSAnZWagAAFfcP/3gAdceYXhvIYxbi4g3E\nXyfqfr0+TAUDlPUet542zA6lMCmidWb8QdVPryRqQ7BibsDwhyofq4MSyvPs\ngZNfM8Gr3gGXUjSCBDRFHu5OQa2ohxb82Pvx0Xk4Z/U5mM4i++cNEPpNALta\n53KnjT6vT3tyNdsCGqyo8XdBy26c6xrt3aF0kBlKQnt7xegoHFqgF7IgjC1k\nBcUXgU/dZxX4W8VdXjpDy7Rc44Cp1nEyIxNDsi4qne5O2hWtcQx6kSHCkpo1\n7SXRjKzz5GSU69GkabexDRcEc1KeuBAGvbH+ULTEI+cTR+O+/mOt2oObUPnV\nstb7mQJmxv2gBURKuUO+zPamNIKlmrjSDnYIljw6MUFfgVm0PPRnzjimOq9s\nCDaXGgw2IKjFyozR2OASiesTysiP+G/mo2hCQfFbN13618hNyTBV9x9hyLoP\nkb17T5ZisHt9W2K1zLlmP/cG3RdEkQ0IvbVFX2uIIJ0F88/eSEGwV20eq8Xv\n6cGG7Yy94pd+VBGmoktFGumqslwtyguk+3OHsmuf/mZ70MgRBA4gVWbWcI8h\nYkmkjj5S8sIcMbV2+CFp91bz+bVTojIADvJ3eoX/6FHXe87V4uHrgCOkHZnh\nj+5Cw2RFt+GwdxjPavN5TE7iBs7b8J3OK9Oxs5dy7udgru7MIec4K/xwV6l6\nM50F\r\n=7iVc\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDQTTPYGca33M0GoetUm94aZX9C5vuBqGs9OY/z88nsjAIgVTiFA9UlYml03Qn4UvZICj9hEgnleIhg448/funSlHo="}],"size":5193,"noattachment":false,"key":"/use-callback-ref/-/use-callback-ref-1.2.0.tgz"},"maintainers":[{"name":"kashey","email":"thekashey@gmail.com"}],"_npmUser":{"name":"kashey","email":"thekashey@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/use-callback-ref_1.2.0_1570864575940_0.5364888424628591"},"_hasShrinkwrap":false,"readme":"<div align=\"center\">\n  <h1>🤙 use-callback-ref 📞</h1>\n  <br/>\n  The same `useRef` but it will callback: 📞 Hello! Your ref was changed!\n  <br/>\n    <a href=\"https://www.npmjs.com/package/use-callback-ref\">\n      <img src=\"https://img.shields.io/npm/v/use-callback-ref.svg?style=flat-square\" />\n    </a>\n    <a href=\"https://travis-ci.org/theKashey/use-callback-ref\">\n       <img alt=\"Travis\" src=\"https://img.shields.io/travis/theKashey/use-callback-ref/master.svg?style=flat-square\">\n    </a>\n    <a href=\"https://bundlephobia.com/result?p=use-callback-ref\">\n      <img src=\"https://img.shields.io/bundlephobia/minzip/use-callback-ref.svg\" alt=\"bundle size\">\n    </a> \n</div>\n\n---\n\n> Keep in mind that useRef doesn't notify you when its content changes.\n> Mutating the .current property doesn't cause a re-render.\n> If you want to run some code when React attaches or detaches a ref to a DOM node,\n> you may want to use ~~a callback ref instead~~ .... **useCallbackRef** instead.\n\n– [Hooks API Reference](https://reactjs.org/docs/hooks-reference.html#useref)\n\nRead more about `use-callback` pattern and use cases:\n\n- https://dev.to/thekashey/the-same-useref-but-it-will-callback-8bo\n\nThis library exposes helpers to handle any case related to `ref` _lifecycle_\n\n- `useCallbackRef` - react on a ref change (replacement for `useRef`)\n  - `createCallbackRef` - - low level version of `useCallbackRef`\n- `useMergeRefs` - merge multiple refs together creating a stable return ref\n  - `mergeRefs` - low level version of `useMergeRefs`\n- `useTransformRef` - transform one ref to another (replacement for `useImperativeHandle`)\n  - `transformRef` - low level version of `useTransformRef`\n- `useRefToCallback` - convert RefObject to an old callback-style ref\n  - `refToCallback` - low level version of `useRefToCallback`\n- `assignRef` - assign value to the ref, regardless it is RefCallback or RefObject\n\nAll functions are tree shakable, but even together it's **less then 300b**.\n\n# API\n\n💡 Some commands are hooks based, and returns the same refs/functions every render.\nBut some are not, to be used in classes or non-react code.\n\n## useRef API\n\n🤔 Use case: every time you have to react to ref change\n\nAPI is 99% compatible with React `createRef` and `useRef`, and just adds another argument - `callback`,\nwhich would be called on **ref update**.\n\n#### createCallbackRef - to replace React.createRef\n\n- `createCallbackRef(callback)` - would call provided `callback` when ref is changed.\n\n#### useCallbackRef - to replace React.useRef\n\n- `useCallbackRef(initialValue, callback)` - would call provided `callback` when ref is changed.\n\n> `callback` in both cases is `callback(newValue, oldValue)`. Callback would not be called if newValue and oldValue is the same.\n\n```js\nimport { useRef, createRef, useState } from 'react';\nimport { useCallbackRef, createCallbackRef } from 'use-callback-ref';\n\nconst Component = () => {\n  const [, forceUpdate] = useState();\n  // I dont need callback when ref changes\n  const ref = useRef(null);\n\n  // but sometimes - it could be what you need\n  const anotherRef = useCallbackRef(null, () => forceUpdate());\n\n  useEffect(() => {\n    // now it's just possible\n  }, [anotherRef.current]); // react to dom node change\n};\n```\n\n💡 You can use `useCallbackRef` to convert RefObject into RefCallback, creating bridges between the old and the new code\n\n```js\n// some old component\nconst onRefUpdate = (newRef) => {...}\nconst refObject = useCallbackRef(null, onRefUpdate);\n// ...\n<SomeNewComponent ref={refObject}/>\n```\n\n## assignRef\n\n🤔 Use case: every time you need to assign ref manually, and you dont know the shape of the ref\n\n`assignRef(ref, value)` - assigns `values` to the `ref`. `ref` could be RefObject or RefCallback.\n\n```\n🚫 ref.current = value // what if it's a callback-ref?\n🚫 ref(value) // but what if it's a object ref?\n\nimport {assignRef} from \"use-callback-ref\";\n✅ assignRef(ref, value);\n```\n\n## useTransformRef (to replace React.useImperativeHandle)\n\n🤔 Use case: ref could be different.\n`transformRef(ref, tranformer):Ref` - return a new `ref` which would propagate all changes to the provided `ref` with applied `transform`\n\n```js\n// before\nconst ResizableWithRef = forwardRef((props, ref) => <Resizable {...props} ref={(i) => i && ref(i.resizable)} />);\n\n// after\n\nconst ResizableWithRef = forwardRef((props, ref) => (\n  <Resizable {...props} ref={transformRef(ref, (i) => (i ? i.resizable : null))} />\n));\n```\n\n## refToCallback\n\n`refToCallback(ref: RefObject): RefCallback` - for compatibility between the old and the new code.\nFor the compatibility between `RefCallback` and RefObject use `useCallbackRef(undefined, callback)`\n\n## useMergeRefs\n\n`mergeRefs(refs: arrayOfRefs, [defaultValue]):ReactMutableRef` - merges a few refs together\n\nWhen developing low level UI components, it is common to have to use a local ref but also support an external one using React.forwardRef. Natively, React does not offer a way to set two refs inside the ref property. This is the goal of this small utility.\n\n```js\nimport React from 'react';\nimport { useMergeRefs } from 'use-callback-ref';\n\nconst MergedComponent = React.forwardRef((props, ref) => {\n  const localRef = React.useRef();\n  // ...\n  // both localRef and ref would be populated with the `ref` to a `div`\n  return <div ref={useMergeRefs([localRef, ref])} />;\n});\n```\n\n💡 - `useMergeRefs` will always give you the same return, and you don't have to worry about `[localRef, ref]` unique every render.\n\n## mergeRefs\n\n`mergeRefs(refs: arrayOfRefs, [defaultValue]):ReactMutableRef` - merges a few refs together\nis a non-hook based version. Will produce the new `ref` every run, causing the old one to unmount, and be _populated_ with the `null` value.\n\n> mergeRefs are based on https://github.com/smooth-code/react-merge-refs, just exposes a RefObject, instead of a callback\n\n`mergeRefs` are \"safe\" to use as a part of other hooks-based commands, but don't forget - it returns a new object every call.\n\n# Similar packages:\n\n- [apply-ref](https://github.com/mitchellhamilton/apply-ref) - `applyRefs` is simular to `mergeRef`, `applyRef` is similar to `assignRef`\n- [useForkRef](https://react-hooks.org/docs/use-fork-ref) - `useForkRef` is simular to `useMergeRefs`, but accepts only two arguments.\n- [react-merge-refs](https://github.com/gregberge/react-merge-refs) - `merge-refs` is simular to `useMergeRefs`, but not a hook and does not provide \"stable\" reference.\n\n---\n\n> Is it a rocket science? No, `RefObject` is no more than `{current: ref}`, and `use-callback-ref` is no more than `getter` and `setter` on that field.\n\n# License\n\nMIT\n","publish_time":1570864576101,"_cnpm_publish_time":1570864576101,"dist-tags":{"latest":"1.3.3"}}