GithubHelp home page GithubHelp logo

tapayne88 / combine-selectors-redux Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 1.0 9.79 MB

Utility function for combining selectors when working with redux. Much like combineReducers it helps for defining selector functions within the reducers.

JavaScript 15.99% TypeScript 84.01%
redux combining-selectors react javascript reselect functional

combine-selectors-redux's Introduction

combineSelectors(selectors)

CircleCI npm version npm downloads

This module follows the patterns set out by redux with combineReducers. It allows you to colocate your selectors with the reducer state they work on and simplifies their usage. It follows a pattern outlined by Dan Abramov in a free egghead.io lesson here

The basic idea is to use combineSelectors everywhere you use combineReducer as you progressively build up your store state. This allows a convenient way to access more complicated derived state (provided through selector functions) following your store state.

Example:

export const selectors = combineSelectors({
  potato: potatoSelectors,
  tomato: tomatoSelectors
});
// Like combineReducers, this will produce an object where the selectors are called with the section of the redux state it's keyed by
{
  potato: {
    // ... potato selectors
  },
  tomato: {
    // ... tomato selectors
  },
}

The access of your selectors now mirrors your redux state.

Example:

reducers/todos.js

export default function todos(state = {}, action) {
  switch (action.type) {
    case "ADD_TODO":
      return {
        ...state,
        [action.payload.id]: action.payload.text,
      };
    default:
      return state;
  }
}

// Selectors
export const getTodo = (state, id) => state[id];
export const getAllIds = (state) => Object.keys(state);

reducers/counter.js

export default function counter(state = 0, action) {
  switch (action.type) {
    case "INCREMENT":
      return state + 1;
    case "DECREMENT":
      return state - 1;
    default:
      return state;
  }
}

// Selectors
export const getCount = (state) => state;

reducers/index.js

import { combineReducers } from "redux";
import { combineSelectors } from "combine-selectors-redux";
import todos, * as todoSelectors from "./todos";
import counter, * as counterSelectors from "./counter";

export default combineReducers({
  todos,
  counter,
});

export const selectors = combineSelectors({
  todos: todoSelectors,
  counter: counterSelectors,
});

App.js

import { createStore } from "redux";
import reducer, { selectors } from "./reducers/index";

let store = createStore(reducer);
console.log(store.getState());
// {
//   todos: [],
//   counter: 0
// }

console.log(selectors);
// {
//   todos: function() {...},
//   counter: function() {...}
// }

console.log(selectors.todos.getAllIds());
// []

console.log(selectors.counter.getCount());
// 0

combine-selectors-redux's People

Contributors

tapayne88 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

mjurincic

combine-selectors-redux's Issues

Add typescript support

Hi,

Nice and simple utility! I missed the typescript support though. Made a start that works for me, but it still requires support for nested modules. I thought it would be nice to share it with you and the community...

Regards,
Joost

declare module "combine-selectors-redux" {

    type Selector<S, AS extends any[], R> = (state:S, ...args: AS) => R;
    type AnySelector<S> = Selector<S, any[], any>;
    type Module<S> = { [key:string]: AnySelector<S> }
    type ModuleState<M> = M extends Module<infer S> ? S : never
    type CombinedModuleState<CM> = { [K in keyof CM]: ModuleState<CM[K]>};

    type MappedSelector<LS, S, F> = F extends Selector<LS, infer AS, infer R> ? Selector<S, AS, R> : never;
    type MappedModule<M,S> = { [K in keyof M]: MappedSelector<ModuleState<M>, S, M[K]> }
    type CombinedSelectors<CS> = { [K in keyof CS]: MappedModule<CS[K], CombinedModuleState<CS>>}

    export function combineSelectors<T>(module: T): CombinedSelectors<T>
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.