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

use-propagate

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-propagate

Propagates an event to multiple subscribers using React hooks.

  • 0.0.1-main.1d6b316
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.8K
increased by8.43%
Maintainers
1
Weekly downloads
 
Created
Source

use-propagate

Propagates an event to multiple subscribers using React hooks.

Background

This pattern is useful for propagating an event to multiple nodes via a callback mechanism.

Unlike setting a value in a context, data will be passed via callback function. Subscriber can save the value into state and re-render as needed.

How to use

Live demo

The following code snippet would send the focus to the text box when the button is tapped.

import { createPropagation } from 'use-propagate';

// Creates a namespace for the propagation. This should be placed outside of the component.
const { Provider, useListen, usePropagate } = createPropagation<void>();

const FocusButton = () => {
  const propagate = usePropagate();

  // When tapped, it will trigger all subscribers.
  const handleClick = useCallback(() => propagate(), [propagate]);

  return (
    <button autoFocus={true} onClick={handleClick}>
      Tap to focus to the text box
    </button>
  );
};

const TextBox = () => {
  const ref = useRef<HTMLInputElement>(null);

  // When the callback is called, send the focus to the text box.
  const handleListen = useCallback(() => ref.current?.focus(), [ref]);

  // Listens to the propagation.
  useListen(handleListen);

  return <input ref={ref} type="text" />;
};

render(
  <Provider>
    <FocusButton />
    <TextBox />
  </Provider>
);

API

export function createPropagation<T>(): {
  Provider: ComponentType;
  useListen: (callback: (value: T) => void) => void;
  usePropagate: (value: T) => void;
};

Behaviors

Why not passing values via useContext?

When propagating a value via useContext, subscribing nodes will be re-rendered. This behavior may not be desirable for events and certain type of scenarios.

How to get response from the listener or wait for the listener to complete?

Modifies the passing value by following the FetchEvent.respondWith pattern or ExtendableEvent.waitUntil pattern.

How to re-render when triggered?

Use the following code snippet to save the value to a state, which will cause a re-render.

const MyComponent = () => {
  const [value, setValue] = useState();

  // When triggered, saves the value to state.
  useListen(value => setValue(value));

  return (<p>The value is {value}</p>.
};

Contributions

Like us? Star us.

Want to make it better? File us an issue.

Don't like something you see? Submit a pull request.

Keywords

FAQs

Package last updated on 25 Mar 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

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