use-mapped-state
v0.0.1
Published
A React hook for mapping state on each state change
Downloads
63
Readme
A React hook for mapping state on each state change
Usage
import useMappedState from 'use-mapped-state';
const Comp = () => {
const [state, setState, [value1, yoValue]] = useMappedState(
'initialValue',
value => value + 1,
value => 'Yo' + value
);
return (
<div>
<span>{state}</span>
<span>{value1}</span>
<span>{yoValue}</span>
<button onClick={() => setState('Cool')} />
</div>
);
};
export default Comp;
Typings
The cool thing about this hook is that you get a typed return value of your map functions
const [
num, // number
setNum, // Dispatch..
[
value1, // number
value2, // string
value3, // object
],
] = useMappedState(1, value => value + 1, value => 'Yo' + value, value => ({ value }));