Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

use-subscription

Package Overview
Dependencies
Maintainers
9
Versions
1837
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-subscription

Reusable hooks

  • 0.0.0-experimental-3f3fe0874
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
581K
decreased by-7.66%
Maintainers
9
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 21 Nov 2019

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