Giter VIP home page Giter VIP logo

typed-redux-kit's Introduction

Typed Redux Kit

Toolkit for Redux with Typescript

What the heck is this?

  • Guide for using redux with Typescript
  • Several sweet scaffoldings(createActionCreators, MappedReducer, ...)
import {
  createActionCreator,
  MappedReducer,
  PayloadAction,
  PureAction,
  Reducer,
} from 'typed-redux-kit'

// Prepare actions
enum ActionTypes {
  Plus = 'test_Plus',
  Set = 'test_Set',
}

namespace Actions {
  export interface PlusAction extends PureAction<ActionTypes.Plus> {}
  export interface SetAction extends PayloadAction<ActionTypes.Set, {
    count: number
  }> {}
}

const ActionCreators = {
  plus: createActionCreator<Actions.PlusAction>(ActionTypes.Plus),
  set: createActionCreator<Actions.SetAction>(ActionTypes.Set),
}

// State types and initial state
interface State {
  count: number
}

const initialState: State = {
  count: 0
}

// Create reducer just for plus action only
const plusSubReducer = (state: State, action: Actions.PlusAction) => ({
  ...state,
  count: state.count + 1,
})
// You also can give the type, we prepared, explicitly
const setSubReducer: Reducer<State, Actions.SetAction> = (state, action) => ({
  ...state,
  ...action.payload,
})

// Create reducer(with type filter by generic<ActionTypes>)
const reducer = new MappedReducer<State, ActionTypes>()
  // Just like switch, but our mapped reducer uses cached references!(Native Map)
  // So, it should perform better than switch for most cases.
  .set(ActionTypes.Plus, plusSubReducer)
  .set(ActionTypes.Set, setSubReducer)
  // Also, These wrong usage will thorw compiler errors
  // because our reducer is smart enough.
  //
  // Wrong ActionType and Sub Reducer combination
  //
  // .set(ActionTypes.Set, plusSubReducer)
  // .set(ActionTypes.Plus, setSubReducer)
  //
  // Invalid Action Types
  // .set('UNWELCOMMED', plusSubReducer)

// Reducer has a reduce method, `reducer.reduce`. Just pass it to our store!
const store = createStore(reducer.reduce, initialState)

test('MappedReducer', () => {
  expect(reducer.get(ActionTypes.Plus)).toEqual(plusSubReducer)

  const plusAction = ActionCreators.plus()
  store.dispatch(plusAction)
  const firstReducedState = store.getState()
  expect(firstReducedState).toEqual({
    count: 1
  })

  const setAction = ActionCreators.set({
    count: 0,
  })
  store.dispatch(setAction)

  const secondReducedState = store.getState()
  expect(secondReducedState).toEqual({
    count: 0,
  })
})

How to use

Please check specs/*.spec.ts! We've already prepared tons of comments there!

  1. Basic usage & MappedReducer
  2. MappedPipeReducer
  3. MappedUniquePipeReducer

Author & Maintainer

License & Copyright

Licensed under MIT Copyright 2017 Revisolution

typed-redux-kit's People

Contributors

rokt33r avatar

Watchers

 avatar

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.