New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

use-subscription

Package Overview
Dependencies
Maintainers
4
Versions
2012
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-subscription

Reusable hooks

1.10.0
latest
Source
npm
Version published
Weekly downloads
547K
6.39%
Maintainers
4
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 05 Dec 2024

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