{"name":"use-callback-ref","version":"1.0.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","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"],"gitHead":"56b1b452d09b115183ece7d99db1ed04547c2261","bugs":{"url":"https://github.com/theKashey/kashe/issues"},"homepage":"https://github.com/theKashey/kashe#readme","_id":"use-callback-ref@1.0.0","_npmVersion":"6.4.1","_nodeVersion":"10.15.3","_npmUser":{"name":"kashey","email":"thekashey@gmail.com"},"dist":{"integrity":"sha512-pPzmRE7Hau61IIX8jdD/tPvW+Rngai2I2w6OX5uIlCBInnw1Scy6KWQZs8gQMqIJDNr5BH7Ub2x7VM9ET/NDVg==","shasum":"fd63707b5455cc92b3dc416e2ed6ed7a861992c7","tarball":"http://registry-npm.ywwl.com/use-callback-ref/download/use-callback-ref-1.0.0.tgz","fileCount":14,"unpackedSize":4891,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdDKSOCRA9TVsSAnZWagAArt4P/19NltSzKGS62xdgYu65\nkONBfY7KWrrn08kp0rnyJsY5FVklH1EU2ENqfKwqZ2jxtHTFHGW23VHxWFMl\nOFsqeIFkXMbCfXhKZQdg4llILSfqsSHftAnd/8X77cAAwQiVG/ijE0YHpYu7\ndLVYEn4sf9YoletDGdkeRXt1vWBQHuOd0ggAUD2t2+1g5RysBJbZriunjvD8\nOT0Y2cZFwliXet892Kf/y1Hj7nNVQ8m9JqQTE/ngCf1Y0sZn9ra+daC5EfPa\n8uK5ZjZ2ofG56UlnxOySGd7NQn63ImGex+Do2cZ+0DgnMe6GOXSxpXayl0DC\nhsHrZKuySe2HyP7o96hblCRwH4qNJVW2wrC23FliH+/QmiCN7zSALQA3j7xq\n7Wdv1Mx9dWG1p37kvw8GKqR+08xbQ5s8wau0HMj7Pfw9y7nz+2w9WjsG61F+\nq7488AbtAkygfGjGGpskx363ZCvKFypmnP+qVPa4CqYmFIiPx8BSf670+3y+\nTjAqtGWNHH/ZrCV0/cB+/Pabg9m59Ug+thd+Vb7NUKuQkkRpi7SLwEnj4i8e\nBgXTTKiUaJg5vGKgNfnZEv+P3Qssr3VBzTPNg6Tjk98YrPuwTQWpyYQPsG66\nhetf3z6jHdQH5I3yDo5Ak03ZJxv6Nv5lUcltvuj6OqE3w/GCnw5IkNOhkMfO\n4kgG\r\n=hEJQ\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDnur9OfGqcUKm5I11DGus5uY/10f3OqpWtgpdLS2lQ5AIhAKgWquwZCdEyFtDaK+6K1eNO6DqTc5rFvdGOOnTV63za"}],"size":1954,"noattachment":false,"key":"/use-callback-ref/-/use-callback-ref-1.0.0.tgz"},"maintainers":[{"name":"kashey","email":"thekashey@gmail.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/use-callback-ref_1.0.0_1561109646096_0.5228201644096"},"_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":1561109646376,"_cnpm_publish_time":1561109646376,"dist-tags":{"latest":"1.3.3"}}