Socket
Socket
Sign inDemoInstall

use-subscription

Package Overview
Dependencies
Maintainers
8
Versions
1734
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-subscription

Reusable hooks


Version published
Weekly downloads
680K
increased by7.68%
Maintainers
8
Weekly downloads
 
Created

What is use-subscription?

The use-subscription npm package is a React hook that allows you to subscribe to external data sources and automatically re-render your component when the data changes. It is particularly useful for integrating with state management libraries or any other data sources that can change over time.

What are use-subscription's main functionalities?

Basic Subscription

This feature allows you to create a basic subscription to an external data source. The `useSubscription` hook takes a subscription object with `getCurrentValue` and `subscribe` methods. The component will re-render whenever the data source changes.

const { useSubscription } = require('use-subscription');

const mySubscription = {
  getCurrentValue: () => myDataSource.getCurrentValue(),
  subscribe: (callback) => {
    myDataSource.subscribe(callback);
    return () => myDataSource.unsubscribe(callback);
  }
};

function MyComponent() {
  const value = useSubscription(mySubscription);
  return <div>{value}</div>;
}

Subscription with Cleanup

This feature demonstrates how to use the `useSubscription` hook with a cleanup function. The `useEffect` hook is used to perform cleanup when the component unmounts, ensuring that any resources are properly released.

const { useSubscription } = require('use-subscription');

const mySubscription = {
  getCurrentValue: () => myDataSource.getCurrentValue(),
  subscribe: (callback) => {
    myDataSource.subscribe(callback);
    return () => myDataSource.unsubscribe(callback);
  }
};

function MyComponent() {
  const value = useSubscription(mySubscription);
  React.useEffect(() => {
    return () => {
      myDataSource.cleanup();
    };
  }, []);
  return <div>{value}</div>;
}

Other packages similar to use-subscription

FAQs

Package last updated on 12 Nov 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc