{"name":"use-subscription","version":"0.0.0-experimental-e1ad4aa36-20230601","license":"MIT","_id":"use-subscription@0.0.0-experimental-e1ad4aa36-20230601","maintainers":[{"name":"gaearon","email":"dan.abramov@gmail.com"},{"name":"gnoff","email":"jcs.gnoff@gmail.com"},{"name":"fb","email":"opensource+npm@fb.com"},{"name":"sophiebits","email":"npm@sophiebits.com"},{"name":"react-bot","email":"react-core@meta.com"}],"homepage":"https://github.com/facebook/react#readme","bugs":{"url":"https://github.com/facebook/react/issues"},"dist":{"shasum":"183abc518a81a2ea503c1bbac0392ef2e93bcd4f","tarball":"http://registry-npm.ywwl.com/use-subscription/download/use-subscription-0.0.0-experimental-e1ad4aa36-20230601.tgz","fileCount":6,"integrity":"sha512-PSpRLHUzGg4wMZAPLBqtPh0mXQqZ6LVgYaVcvLG4Q1jU5Rgc2NpjGa614ac5sv2OlTJiqaDvcyFdtBMe03WXWw==","signatures":[{"sig":"MEYCIQCWFS4VAZ461fbiyRqalFca1yOPN7P85CirVkwY/w/6zAIhAPYSWWRhhtHcJklft3qCK99mQdcVKBblck6JwUu7/8y+","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":7340,"size":2909,"noattachment":false,"key":"/use-subscription/-/use-subscription-0.0.0-experimental-e1ad4aa36-20230601.tgz"},"readme":"# use-subscription\n\nReact Hook for subscribing to external data sources.\n\n**You may now migrate to [`use-sync-external-store`](https://www.npmjs.com/package/use-sync-external-store) directly instead, which has the same API as [`React.useSyncExternalStore`](https://react.dev/reference/react/useSyncExternalStore). The `use-subscription` package is now a thin wrapper over `use-sync-external-store` and will not be updated further.**\n\n# Installation\n\n```sh\n# Yarn\nyarn add use-subscription\n\n# NPM\nnpm install use-subscription\n```\n\n# Usage\n\nTo configure a subscription, you must provide two methods: `getCurrentValue` and `subscribe`.\n\nIn order to avoid removing and re-adding subscriptions each time this hook is called, the parameters passed to this hook should be memoized. This can be done by wrapping the entire subscription with `useMemo()`, or by wrapping the individual callbacks with `useCallback()`.\n\n## Subscribing to event dispatchers\n\nBelow is an example showing how `use-subscription` can be used to subscribe to event dispatchers such as DOM elements.\n\n```js\nimport React, { useMemo } from \"react\";\nimport { useSubscription } from \"use-subscription\";\n\n// In this example, \"input\" is an event dispatcher (e.g. an HTMLInputElement)\n// but it could be anything that emits an event and has a readable current value.\nfunction Example({ input }) {\n\n  // Memoize to avoid removing and re-adding subscriptions each time this hook is called.\n  const subscription = useMemo(\n    () => ({\n      getCurrentValue: () => input.value,\n      subscribe: callback => {\n        input.addEventListener(\"change\", callback);\n        return () => input.removeEventListener(\"change\", callback);\n      }\n    }),\n\n    // Re-subscribe any time our input changes\n    // (e.g. we get a new HTMLInputElement prop to subscribe to)\n    [input]\n  );\n\n  // The value returned by this hook reflects the input's current value.\n  // Our component will automatically be re-rendered when that value changes.\n  const value = useSubscription(subscription);\n\n  // Your rendered output goes here ...\n}\n```\n\n## Subscribing to observables\n\nBelow are examples showing how `use-subscription` can be used to subscribe to certain types of observables (e.g. RxJS `BehaviorSubject` and `ReplaySubject`).\n\n**Note** that it is not possible to support all observable types (e.g. RxJS `Subject` or `Observable`) because some provide no way to read the \"current\" value after it has been emitted.\n\n### `BehaviorSubject`\n```js\nconst subscription = useMemo(\n  () => ({\n    getCurrentValue: () => behaviorSubject.getValue(),\n    subscribe: callback => {\n      const subscription = behaviorSubject.subscribe(callback);\n      return () => subscription.unsubscribe();\n    }\n  }),\n\n  // Re-subscribe any time the behaviorSubject changes\n  [behaviorSubject]\n);\n\nconst value = useSubscription(subscription);\n```\n\n### `ReplaySubject`\n```js\nconst subscription = useMemo(\n  () => ({\n    getCurrentValue: () => {\n      let currentValue;\n      // ReplaySubject does not have a sync data getter,\n      // So we need to temporarily subscribe to retrieve the most recent value.\n      replaySubject\n        .subscribe(value => {\n          currentValue = value;\n        })\n        .unsubscribe();\n      return currentValue;\n    },\n    subscribe: callback => {\n      const subscription = replaySubject.subscribe(callback);\n      return () => subscription.unsubscribe();\n    }\n  }),\n\n  // Re-subscribe any time the replaySubject changes\n  [replaySubject]\n);\n\nconst value = useSubscription(subscription);\n```\n","gitHead":"e1ad4aa3615333009d76f947ff05ddeff01039c6","_npmUser":{"name":"react-bot","email":"react-core@meta.com"},"repository":{"url":"git+https://github.com/facebook/react.git","type":"git","directory":"packages/use-subscription"},"_npmVersion":"8.11.0","description":"Reusable hooks","directories":{},"_nodeVersion":"16.16.0","dependencies":{"use-sync-external-store":"0.0.0-experimental-e1ad4aa36-20230601"},"_hasShrinkwrap":false,"readmeFilename":"README.md","devDependencies":{"rxjs":"^5.5.6"},"peerDependencies":{"react":"0.0.0-experimental-e1ad4aa36-20230601"},"_npmOperationalInternal":{"tmp":"tmp/use-subscription_0.0.0-experimental-e1ad4aa36-20230601_1685722516517_0.4037115783250036","host":"s3://npm-registry-packages"},"publish_time":1685722516690,"_cnpm_publish_time":1685722516690,"dist-tags":{"beta":"1.10.0-beta-26f2496093-20240514","canary":"1.10.0-rc-d6cb4e77-20240911","experimental":"0.0.0-experimental-d6cb4e77-20240911","latest":"1.8.2","next":"1.10.0-rc-d6cb4e77-20240911","rc":"1.10.0-rc-d6cb4e77-20240911"}}