Skip to main content
Version: 1.1.x

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'}
}, [])
}