Skip to content

maxhallinan/zelektree

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

zelektree

Build status Coverage Status

Embed selectors in a Redux state tree.

This is a convenient way for components to share selectors that are used widely throughout an app.

Inspired by Baobab.

Install

$ yarn install zelektree

Usage

import { connect, createStore, } from 'redux';
import { embedSelectors, } from 'zelektree';

const initialState = { foo: null, };

const reducer = (state = initialState) => state;

const store = createStore(
  reducer,
  // pass as the `enhancer` argument to `createStore`
  embedSelectors({
    // key each selector by intended state key name
    isFoo: (state) => !!state.foo,
  }),
);

// state and derived state are synced and merged
const state = store.getState(); // { foo: null, isFoo: false, }

API

embedSelectors(selectors)

Takes an object map of selectors and returns a Redux StoreEnhancer.

The store enhancer refreshes the selectors each time getState is called and the result is merged with the returned state.

embedSelectors expects the top-level state value to be an object. To optimize performance, this expectation is not enforced with runtime checks.

selectors

Type: { [String]: (Object) -> * }

An object map of selectors. The key for each selector is used as the state tree key for that selector's output. A selector is called with a single argument, state. state is the current state of the store.

embedSelectors is interoperable with any selector that is soley a reduction of state and not state and props, for example.

License

MIT © Max Hallinan

About

Embed selectors in a Redux state tree.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published