Skip to content

Latest commit

 

History

History
136 lines (87 loc) · 3.36 KB

docs.md

File metadata and controls

136 lines (87 loc) · 3.36 KB

Table of Contents

selectorForSlice

Given the path of a certain state slice, returns a function that can be used to create state selectors (helpful for mapStateToProps()).

Parameters

  • slicePath String Path to slice of state.

Examples

import { selectorForSlice } from 'lp-redux-utils'

const state = {
  userSlice: {
    user: {
      name: 'test'
    }
  }
}

const select = selectorForSlice('userSlice')

// The resulting select() function has arguments (path, defaultValue)

const selectors = {}
selectors.user = select('user')
selectors.username = select('user.name', 'defaultName')

export { selectors }

// These selectors can be called in mapStateToProps() like so:
// selectors.user(state) => { name: 'test' }
// selectors.username(state) => 'test'


*

Returns Function A function that can be used to create state selectors.

setState

A helper function for creating simple "setter" reducers. Given a path, it sets the state at that path to the payload of an action.

Parameters

  • path String Path to the part of the state that will be set
  • transform Function A function with arguments (action, state, slice) that can be used to transform the value that will be set. slice is the data, if any, that already exists in the state at path. The default transform function simply returns the action's payload. To set the state to a constant value, simply pass the value in place of the transform function.

Examples

import { setState } from 'lp-redux-utils'
import { createAction, handleActions } from 'redux-actions'

const setCount = createAction('SET_COUNT')
const setCountInverse = createAction('SET_COUNT_INVERSE')
const doubleExistingCount = createAction('DOUBLE_EXISTING_COUNT')

const reducer = handleActions({
  [setCount]: setState('count'),
  [setCountInverse]: setState('count', action => action.payload * -1)
  [doubleExistingCount]: setState('count', (action, state, count) => count * 2)
  [resetCount]: setState('count', 0)
})

*

Returns Function A function that can be used in a reducer to handle an action.

unsetState

A helper function for creating simple reducers that "unset" a piece of state. Given a path, it calls lodash unset on the state at that path.

Parameters

  • path String Path to the part of state to unset

Examples

import { unsetState } from 'lp-redux-utils'
import { createAction, handleActions } from 'redux-actions'

const clearCount = createAction('CLEAR_COUNT')

const reducer = handleActions({
  [clearCount]: unsetState('count'),
})

*

Returns Function A function that can be used in a reducer to handle an action.