This package has been deprecated

Author message:

WARNING: This project has been renamed to undux. Install using 'npm install undux --save' instead.

babydux
TypeScript icon, indicating that this package has built-in type declarations

3.0.1 • Public • Published
Babydux

Build Status npm mit

A paper-thin, 100% typesafe Redux for babies

Install

# Using Yarn: 
yarn add babydux
 
# Or, using NPM: 
npm install babydux --save

Use

1. Create a store

import { connect, createStore } from 'babydux'
 
// If you're using Babydux with TypeScript, declare your store's types.
type Store = {
  today: Date
  users: string[]
}
 
// Create a store with an initial value.
let store = createStore<Store>({
  today: new Date,
  users: []
})
 
export let withStore = connect(store)

Be sure to define a key for each value in your model, even if the value is initially undefined.

2. Connect your React components

import { withStore } from './store'
 
                            // Update the component when `today` changes
let MyComponent = withStore('today')(({ store }) =>
  <div>
    Hello! Today is {store.get('today')}
    <button onClick={() => store.set('today')(new Date)}>Update Date</button>
  </div>
)

That's all there is to it.

Features

Effects

Though Babydux automatically updates your model for you, it also lets you listen on and react to model updates (similarly to how vanilla Redux lets you subscribe to Actions). Babydux subscriptions are full Rx observables, so you have fine control over how you react to a change:

store
  .on('today')
  .filter(() => _.getTime() % 2 === 0) // only even timestamps
  .debounce(100)
  .subscribe(_ => console.log('Date changed', _))

Lensed connects

Instead of updating your React component when anything on the model changed, you subscribe just to specific properties on your model. Let's modify our React example to only update when today changes:

let MyComponent = withStore('today')(
  ({ store }) => ...
)

Everything is the same as before, I just added 'today' as an argument to the function returned by connect.

Partial application all the way through

Partially apply the connect function to yield a convenient withStore function:

let withStore = connect(store)

Or, partially apply the set function to yield a convenient setter:

let setUsers = store.set('users')
setUsers(['amy'])
setUsers(['amy', 'bob'])

Built-in logger

If you create your store with withLogger higher order store, all model updates (which key was updated, previous value, and new value) will be logged to the console.

To enable the logger, import withLogger and wrap your store with it:

import { createStore, withLogger } from 'babydux'
 
let store = withLogger(createStore<Store>({...}, true))

And logs look like this:

Plugins

Babydux is easy to modify with plugins (also called "higher order stores"). Just define a function that takes a store as an argument and returns a store, adding listeners along the way. For convenience, Babydux supports 2 types of listeners for plugins:

import { createStore, Plugin } from 'babydux'
 
let withLocalStorage: Plugin = store => {
 
  // Listen on an event
  store.onAll().subscribe(_ =>
    console.log('something changed!', _)
  )
 
  // Listen on an event (fires before the model is updated)
  store.beforeAll().subscribe(({ key, previousValue, value}) =>
    localStorage.set(key, value)
  )
 
}

Babydux also supports .on() and .before(), but because a plugin doesn't know what Actions will be bound to it, these are generally unsafe to use.

Design philosophy

Goal #1 is total type-safety.

Getting, setting, reading, and listening on model updates is 100% type-safe: use a key that isn't defined in your model or set a key to the wrong type, and you'll get a compile-time error. And connected components are just as type-safe.

Goal #2 is letting you write as little boilerplate as possible.

Babydux is like Redux, but reducers are already baked-in. Babydux automatically creates an action and a reducer for each key on your state, so you don't have to write tedious boilerplate. Babydux still emits Actions under the hood (which you can listen on to produce effects), but gives you an incredibly simple get/set API that covers most use cases.

If you're using Babydux with the provided React connector, Babydux will update your React component any time a reducer fires (just like React-Redux). You can optionally filter on specific state keys that you care about for more targeted updates.

Tests

yarn test

License

MIT

Package Sidebar

Install

npm i babydux

Weekly Downloads

0

Version

3.0.1

License

MIT

Last publish

Collaborators

  • bcherny