# redux-key-listener

> A Redux middleware that listens specific keys and triggers specific functions.

Latest version **1.0.2** (published 2020-09-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install redux-key-listener
pnpm add redux-key-listener
yarn add redux-key-listener
bun add redux-key-listener
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2020-09-18 |
| First published | 2020-09-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 4.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | paranoidaf |
| Keywords | redux, react, middleware, redux-middleware, listener, key-listener |

## Links

- npm: https://www.npmjs.com/package/redux-key-listener
- Repository: https://github.com/Paranoid-AF/redux-key-listener
- npm.io page: https://npm.io/package/redux-key-listener

## Dependencies (1)

- [object-keys-diff](https://npm.io/package/object-keys-diff.md) ^2.0.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2020-09-18
- 1.0.1 — 2020-09-18
- 1.0.0 — 2020-09-18

## README

# Redux Key Listener
This is a simple middleware for Redux, which detects changes in state for specific keys that you specified, then trigger whatever you want if things get different.

## Installation
Run the following command:
```
npm i redux-key-listener --save-dev
```
or
```
yarn add redux-key-listener --dev
```
(Depending on which package manager you're using)

## Usage
Steps are below.
### Applying the middleware
When you're creating a store, apply this middleware to it.
```javascript
import keyListenerGenerator from 'redux-key-listener' // You may want to import it first.

const store = createStore(
  reducers,
  applyMiddleware(
    thunk,
    keyListenerGenerator(ListenerList) // Apply this middleware.
  )
)
```
Wait, you might get an error since you haven't created the `ListenerList` yet. Don't panic, let's move on.

### Creating Listener and ListenerList
`ListenerList` contains multiple instances of `Listener`.
#### ListenerList
**ListenerList** is an object that contains a key-value pair of **a string of key that you want to listen to** and **Listener - a function to handle changes**.

It may look like this:
```javascript
const ListenerList = {
  isVisible: function(){ /* I am a Listener */},
  'user.info.age': function(){ /* I am a Listener */},
  'user.token': function(){ /* I am a Listener */}
}
```
The key in `ListenerList` is the key path in your store's state of Redux, you need to divide the keys with dot to simulate a path.

#### Listener
**Listener** is simply a function that you declare to handle changes.
```typescript
type Listener = (currentValue?: any, previousValue?: any, currentStore?: any, previousStore?: any) => any
```
- `currentValue` is the current value of the key that you're listening here.
- `previousValue` is the previous value, before the action gets dispatched.
- `currentStore` is the current store of Redux.
- `previousStore` is the previous store of Redux, before the action gets dispatched.

You could write something like this:

```javascript
const ListenerList = {
  isVisible: function(currentValue){
    console.log(`Visibility: ${currentValue}`)
  },
  ...
}
```

Now as long as `isVisible` in your Redux's store's state changes, this function will be triggered and make an output to your console.

> PS: You could skip as many arguments as you want, or even include no argument at all.

## TypeScript
This module does support TypeScript, and has definition file built in. Check it out if needed.

---
_Source: https://npm.io/package/redux-key-listener · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
