https://github.com/kodedninja/choo-shortcut
Shortcut access to parts of the state anywhere in your app
https://github.com/kodedninja/choo-shortcut
Last synced: 24 days ago
JSON representation
Shortcut access to parts of the state anywhere in your app
- Host: GitHub
- URL: https://github.com/kodedninja/choo-shortcut
- Owner: kodedninja
- Created: 2019-04-11T19:42:27.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-04-11T19:45:01.000Z (about 7 years ago)
- Last Synced: 2025-01-02T22:30:18.416Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 1.95 KB
- Stars: 3
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# choo-shortcut
Shortcut access to parts of the `state` anywhere in your app.
Useful to simulate "contexts", avoid passing down the `state` to hell. It's again a ridiculous little function.
## Installation
```
npm i choo-shortcut
```
## Example
```javascript
// flowers-context.js
var createShortcut = require('choo-shortcut')
var shortcut = createShortcut(function (state, emitter) {
return state.flowers
})
module.exports = shortcut.set
module.exports.getFlowers = shortcut.get
```
In your main file:
```javascript
app.use(require('./flowers-context'))
```
Then anywhere in your app
```javascript
var { getFlowers } = require('../flowers-context')
getFlowers()
// state.flowers that has been set somewhere
```
## API
### `shortcut = createShortcut(reducer(state, emitter))`
Creates a new shortcut (context store). `reducer` is a function which "reduces" the state to the part that should be stored. It gets the typical `state` and `emitter` of a Choo store.
### `shortcut.set(state, emitter)`
The store that should be passed to `app.use`.
### `shortcut.get()`
Returns the current value that's stored.