npm.io
0.1.2 • Published 7 years ago

use-combined-state

Licence
MIT
Version
0.1.2
Deps
0
Size
3 kB
Vulns
0
Weekly
0
Stars
6

use-combined-state

Motivation

Restore how setState is used before React without hooks. Hooks is awesome. However setState should be passed full state as value returned in Redux reducer. You may also find grouping state under a single "namespace" improve readability.

Usage

import useCombinedState from 'use-combined-state';

// ...

const [ state, setState ] = useCombinedState({ visible: true, data: { titi: 'toto' } });


// set partial state to update, preserve `data` field.
setState({
  visible: false,
})

setState((prevState) => ({
  visible: !prevState.visible, // can pass a function which sets partial state
}))

<SomeComponent visible={state.visible}>

Keywords