{"name":"use-callback-ref","version":"1.2.2","description":"The same useRef, but with callback","main":"dist/es5/index.js","sideEffects":false,"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.4 KB"},{"path":"dist/es2015/useRef.js","limit":"0.2 KB"},{"path":"dist/es2015/useMergeRef.js","limit":"0.2 KB"}],"dependencies":{"@types/react":"^16.9.11"},"gitHead":"783b487062bc7fc1fa8766464a08956133e7e86a","bugs":{"url":"https://github.com/theKashey/kashe/issues"},"homepage":"https://github.com/theKashey/kashe#readme","_id":"use-callback-ref@1.2.2","_nodeVersion":"12.16.1","_npmVersion":"6.13.4","dist":{"integrity":"sha512-s7WLAjMmcgSQ6ocTv93xfljIhpIi6r7f+bH54BX7eP26c0Q+W0iemfFBHYND2cSfaWpweiN0/NVtXVMKRlktdg==","shasum":"43f6270a2979915d0b0c715581859026c68ad5f8","tarball":"http://registry-npm.ywwl.com/use-callback-ref/download/use-callback-ref-1.2.2.tgz","fileCount":42,"unpackedSize":18937,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJekRfYCRA9TVsSAnZWagAAEiwQAJtf16Bi6DFfVsWDRXo+\nT2nYrWXUhcbpFQtuxSne/sUEyCxAPGc6ARFeckeIooR+zlmY0ambXo3ZH69s\n3FF7UA2/IlZzyM3G4vVYZH/L2hrMp/MXT+17erz7Awzlff3ek5mVCylUjt6A\nVtJpFI0IHeJbKB4+YoztTE4WF5+NOcXCw8ItBo03KPsZcAo9RBu8RD03AtZ/\nygIiXEX6Mgkp/s4XJW6dmsay1LmmYPPL89GMY+HYVxuLIHKXxYERFlVb3vMh\n3pzjpBZYQXw5zZyb+ADGsKbbz9yZbweqIKZsc1L5ZxYnKwUHNsGOUNB4zo4i\nn5AruKmAy8aYFmYEEp1R1vxUxsgtejyfUpCokFug0t3KD5jDoasR395teUSW\n6CcoY5OTPsW36Wsz9Rf9WOzIPNp8Vco9ufOT/KXdJEviIbz/ko1x63G44sDW\nqOmtI3AgLIgF8MO0owriQzOuSE4TBsc2FqpYtX5NyqUcHdkjsNtaTieKAgGH\nJRtb/enFNvubW5+ok3yWkFTEbQcc9groyc7jyv7NUBcoDs3mf8NUid8E6xy8\nUZbOQyi2j+vDaED6SWvaD+Kt2O8OyxnASTUsSJdQ5kRNn/79AzFbKfkVYNwX\noHZvw/+jUsJk2bjE26PBfrfL/m2DMOjY7wCd43/JlLF0xKYqXFQ7dcVGOrnq\nAh5R\r\n=3rQ0\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCg1nJSI9qrSw6m3AdqmceD3w0aVka0K2npW7SKl7dX7wIhAPNo2w/pD/lP3CRQssmZHOtmuLOrRc5xAE3Z84UvMTaU"}],"size":5268,"noattachment":false,"key":"/use-callback-ref/-/use-callback-ref-1.2.2.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.2_1586567127162_0.9390573120272478"},"_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":1586567127306,"_cnpm_publish_time":1586567127306,"dist-tags":{"latest":"1.3.3"}}