Skip to content

Latest commit

 

History

History
46 lines (38 loc) · 828 Bytes

action.md

File metadata and controls

46 lines (38 loc) · 828 Bytes
id title
action
action()

Action creator for dispatching setState action for a given state path.

action(statePath, payload, setter?: (state, payload) => newState)

Returns

setState redux action

action(statePath, payload, setter)
// { type: SET_REDUX_STATE, payload, path, reducer }

Example

import { useEffect } from 'react'
import useReduxState, { action } from 'use-redux-states'
const Component = () => {
  useReduxState({
    state: {
      state1: 'a',
      state2: 'b'
    },
    path: 'component_state'
  })

  useEffect(() => {
    store.dispatch(
      action(
        'component_state',
        { state2: 'c' },
        (component_state, payload) => ({ ...component_state, ...payload })
      )
    )
    // component_state {state1: 'a', state2: 'c'}
  }, [])
}