{"name":"use-callback-ref","version":"1.2.5","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/use-callback-ref.git"},"author":{"name":"theKashey","email":"thekashey@gmail.com"},"license":"MIT","dependencies":{},"devDependencies":{"ts-react-toolbox":"^0.2.11"},"peerDependencies":{"react":"^16.8.0 || ^17.0.0","@types/react":"^16.8.0 || ^17.0.0"},"peerDependenciesMeta":{"@types/react":{"optional":true}},"engines":{"node":">=8.5.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"}],"gitHead":"4df158d172c421ca27e39a610303e333ba689060","bugs":{"url":"https://github.com/theKashey/use-callback-ref/issues"},"homepage":"https://github.com/theKashey/use-callback-ref#readme","_id":"use-callback-ref@1.2.5","_nodeVersion":"12.19.0","_npmVersion":"6.14.8","dist":{"integrity":"sha512-gN3vgMISAgacF7sqsLPByqoePooY3n2emTH59Ur5d/M8eg4WTWu1xp8i8DHjohftIyEx0S08RiYxbffr4j8Peg==","shasum":"6115ed242cfbaed5915499c0a9842ca2912f38a5","tarball":"http://registry-npm.ywwl.com/use-callback-ref/download/use-callback-ref-1.2.5.tgz","fileCount":42,"unpackedSize":32691,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgA5Z+CRA9TVsSAnZWagAAQmMP/0KYGZ4GdscBSDvSH2bR\nhBQ+qmhH/TVguU820c43nOkLxUuICllW623ruSUkNdo0gSli1WxUCmHsMsvQ\n05z6nSxhdcv3tOjsWdcb60lG6mPWvUFpFVZlPCsqOUWUYZcuKrzE0A7fiw+Z\nrOzAmwa7tXlvoLt63WlPRPd/5xl7v9yHRovnNEAA8k9N7XnW8WOiCwkALJ53\n0wTV5WhyrjvXKXLHK1Wyh+V9+0CbhVAkWF9/kpRvHVgOmmn4qt4gplbqK9K5\nLjUHaHt6NEg8W7c41YA+oWyhtslSLTDZOwF2w0FigH99LT12zJVbAN8T4th5\nmdVcrbi7ldy0HrN9iBcc47VBp/B3P4oDlQsq6iiqs3eFpWn7iRl9TUbPUswx\nhMIJo+aQAwG6ZHKyoykuNHtWj473r4B8UJUI5bi7TKqsXsrDlVypi01Js2V1\nvarO3EwbvCRgKuM/TYV4F/1OEgSu8MZ8QfUXVGzJpIkR6cv2Dm1C+AhfRi29\niDkKEvGw7zkfTSGoOlxFXLcGKY/R76NeBj4Wx9xeKpXpEg4Fj8QE5rAUQ7JZ\nuUcPjlK1XAGXaUrK8/1T0EOhC0Cg/dTdS72XSYtTZKnwCOTLitzeE74Pu9bz\nHNe//CUO5WdSEBPrJ34mf4L2nibQUpgYtwcfGn3ullPpmDtv6Gswir4IU+er\nIFCr\r\n=Dbxq\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQD4csrmb92rOgGCUirwvZbBVuIFhZFeEf7+nKfAecETDgIgLpwENCkB8jQb160ob66z4o5SOtujs7LbN5p1HO5tqbI="}],"size":7177,"noattachment":false,"key":"/use-callback-ref/-/use-callback-ref-1.2.5.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.2.5_1610847869777_0.17275788629188793"},"_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":1610847869890,"_cnpm_publish_time":1610847869890,"dist-tags":{"latest":"1.3.3"}}