{"name":"use-callback-ref","version":"1.3.0","description":"The same useRef, but with callback","main":"dist/es5/index.js","jsnext:main":"dist/es2015/index.js","module":"dist/es2015/index.js","types":"dist/es5/index.d.ts","module:es2019":"dist/es2019/index.js","sideEffects":false,"scripts":{"dev":"lib-builder dev","test":"jest","test:ci":"jest --runInBand --coverage","build":"lib-builder build && yarn size:report","release":"yarn build && yarn test","size":"npx size-limit","size:report":"npx size-limit --json > .size.json","lint":"lib-builder lint","format":"lib-builder format","update":"lib-builder update","prepublish":"yarn build && yarn changelog","changelog":"conventional-changelog -p angular -i CHANGELOG.md -s","changelog:rewrite":"conventional-changelog -p angular -i CHANGELOG.md -s -r 0"},"repository":{"type":"git","url":"git+https://github.com/theKashey/use-callback-ref.git"},"author":{"name":"theKashey","email":"thekashey@gmail.com"},"license":"MIT","dependencies":{"tslib":"^2.0.0"},"devDependencies":{"@theuiteam/lib-builder":"^0.1.4","@size-limit/preset-small-lib":"^2.1.6","@types/enzyme-adapter-react-16":"^1.0.6","enzyme-adapter-react-16":"^1.15.6"},"peerDependencies":{"react":"^16.8.0 || ^17.0.0 || ^18.0.0","@types/react":"^16.8.0 || ^17.0.0 || ^18.0.0"},"peerDependenciesMeta":{"@types/react":{"optional":true}},"engines":{"node":">=10"},"keywords":["react","hoot","useRef","createRef"],"husky":{"hooks":{"pre-commit":"lint-staged"}},"lint-staged":{"*.{ts,tsx}":["prettier --write","eslint --fix","git add"],"*.{js,css,json,md}":["prettier --write","git add"]},"prettier":{"printWidth":120,"trailingComma":"es5","tabWidth":2,"semi":true,"singleQuote":true},"gitHead":"43dd323f1820604e36883b8342ec7db9a7c27765","bugs":{"url":"https://github.com/theKashey/use-callback-ref/issues"},"homepage":"https://github.com/theKashey/use-callback-ref#readme","_id":"use-callback-ref@1.3.0","_nodeVersion":"16.3.0","_npmVersion":"7.15.1","dist":{"integrity":"sha512-3FT9PRuRdbB9HfXhEq35u4oZkvpJ5kuYbpqhCfmiZyReuRgpnhDlbr2ZEnnuS0RrJAPn6l23xjFg9kpDM+Ms7w==","shasum":"772199899b9c9a50526fedc4993fc7fa1f7e32d5","tarball":"http://registry-npm.ywwl.com/use-callback-ref/download/use-callback-ref-1.3.0.tgz","fileCount":63,"unpackedSize":48486,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCXoBtoQCvUfnJ3Kd4Ipkb1xxLZ/qX9qG0BHHjZ3wYNPgIhAMwqD6iRqQ/iH8APrHwnN07T6x4U5Rv5qKz96jeDwWXO"}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJiXMtOACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmpxpQ//fhy+bc6+p04aKyN01JR1EKWHIVhPH8ucMTW7hpWKYU/9YTsJ\r\nft2yAK6Idl/8h/d4maqx5G4Em5pyKaI2kWNWIDbWcHtT7Fr3D78KF4L1/MR2\r\nYOocG0/rTqxtD1Y3uJIA+kBDRxYp/6q7nWWFXhLp/peNEXy/T31paEtnKmSm\r\nEmh7NibrTnThRpFcp0da2qvsAFEO7VgskdZlNPVbK8C/cAD8Pn/46yA3DdXF\r\n+o3sKupAy4UFNt044Xaq2HGWtBVp08tWei/FVlKbH8jnFfHiA/eIOAOLapX2\r\nNuYU52oUkgvwu+rupJgHt9rgaEsCudhQ+DGwBNoPvqY+XUgxZ6cTI24HTPIf\r\nl5rxoPIR88EGDZJBgnrOFgWncFgt8hEGT37RSNq6/r4mfIrjAE2G+RqqOY0A\r\n6jfJ8FiFyT0kRwthV1BlrN6Ni/QDnDGIFecok86ZmD+pRjEZYPrKGUA3j/nR\r\nzIpyJVm3IU+g8s5y/ANqp/hXUXhN8ZmOvK5EXcOfTjdbOoKs/0j3M79abs9i\r\nmguMahRNoj/T/MWo7mGz+hvsOlga8Clj0feZmPLf9yXcXVGvbj+mgCtalsmS\r\nJbhuGeYGxWgHeknWBcu0iu+wiDKli+0Tl/dWGHsDQEMVdRxiaH5DN+Y2qgrk\r\n8Y4CBB5DshdKV2pf/ChcxHV0ZVSxlvlc8Wk=\r\n=VvOh\r\n-----END PGP SIGNATURE-----\r\n","size":8808,"noattachment":false,"key":"/use-callback-ref/-/use-callback-ref-1.3.0.tgz"},"_npmUser":{"name":"kashey","email":"thekashey@gmail.com"},"directories":{},"maintainers":[{"name":"kashey","email":"thekashey@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/use-callback-ref_1.3.0_1650248526346_0.32599727441495774"},"_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":1650248526557,"_cnpm_publish_time":1650248526557,"dist-tags":{"latest":"1.3.3"}}