Skip to content

Subscribe to changes in any part of redux state

Notifications You must be signed in to change notification settings

jackgeek/redux-subscriber

 
 

Repository files navigation

redux-subscriber

Build Status codecov npm version

This package allows you to subscribe to changes in any part of Redux state.

Installation

npm install redux-subscriber --save

Usage

store.js

import {createStore} from 'redux';
import initSubscriber from 'redux-subscriber';

const store = createStore(...);

// "initSubscriber" returns "subscribe" function, so you can use it
const subscribe = initSubscriber(store);

somewhere-else.js

// or you can just import "subscribe" function from the package
import {subscribe} from 'redux-subscriber';

const unsubscribe = subscribe('user.messages.count', state => {
    // do something
});

// if you want to stop listening to changes
unsubscribe();

Examples

API

initSubscriber(store) (default export) - initialize redux-subscriber, so after that you can use subscribe method.

Options

  • store - instance of Redux store.

Returns subscribe function.

subscribe(selector, callbackFunction) - subscribe callbackFunction to changes.

Options

  • selector - string or function, string for which specified the part of state (e.g. user.message.count) to listen to, or function that accepts state as the first parameter and returns the state to listen to (i.e. a Redux selector function).
  • callbackFunction - function which will be called when the part of state has changed. New state is passed as a parameter.

Returns unsubscribe function which can be called to unsubscribe from changes.

About

Subscribe to changes in any part of redux state

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%