Socket
Socket
Sign inDemoInstall

redux-state-mapper

Package Overview
Dependencies
0
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    redux-state-mapper

State mapper that map the action types to reducers for Redux


Version published
Weekly downloads
8
increased by100%
Maintainers
1
Install size
5.83 kB
Created
Weekly downloads
 

Readme

Source

StateMapper for Redux

State mapper that map the action types to reducers for Redux

Installation

npm install --save redux-state-mapper

Usage

import stateMapper from 'redux-state-mapper'

const reducer = stateMapper(initialState)
  .when('REQUEST_ACTION', ({ data }, { query }) => ({ query, data }))
  .when('SUCCESS_ACTION', 'FAILURE_ACTION', ({ query }, { data }) => ({ query, data }))

This is completely equivalent to:

const reducer = (state = initialState, action) => {
  switch (action.type) {
    case 'REQUEST_ACTION':
      return { query: action.query, data: state.data }
    case 'SUCCESS_ACTION':
    case 'FAILURE_ACTION':
      return { query: state.query, data: action.data }
    default:
      return state
  }
}

Example

import {combineReducers} from 'redux'
import stateMapper from 'redux-state-mapper'
import {
  CHANGE_LOGIN_PARAMS,
  SUCCESS_LOGIN,
  FAILURE_LOGIN,
  CATCH_AUTH_ERROR,
  SUCCESS_ME,
  FAILURE_ME } from './actions'

const initialUserState = {
    id: null,
    name: null,
    email: null,
}

export default combineReducers({
    token: stateMapper(null)
        .when(SUCCESS_LOGIN, (state, { token }) => token)
        .when(FAILURE_LOGIN, CATCH_AUTH_ERROR, () => null),
    params: stateMapper({})
        .when(CHANGE_LOGIN_PARAMS, (state, { params }) => params),
    error: stateMapper(null)
        .when(SUCCESS_LOGIN, () => null)
        .when(FAILURE_LOGIN, (state, { error }) => error),
    logged: stateMapper(false)
        .when(SUCCESS_LOGIN, () => true)
        .when(FAILURE_LOGIN, () => false),
    user: stateMapper(initialUserState)
        .when(SUCCESS_ME, (state, { payload }) => payload)
        .when(SUCCESS_LOGIN, (state, { user }) => user)
        .when(FAILURE_LOGIN, FAILURE_ME, () => initialUserState)
})

License

MIT

Keywords

FAQs

Last updated on 12 Nov 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc