Skip to content

Instantly share code, notes, and snippets.

@katesroad
Created October 3, 2021 00:41
Show Gist options
  • Save katesroad/2c6f00c9e98e0b815000649435525ac7 to your computer and use it in GitHub Desktop.
Save katesroad/2c6f00c9e98e0b815000649435525ac7 to your computer and use it in GitHub Desktop.
react useReducer
// change can be a function or an object
const reduderFn = (state, change) => ({
...state,
...(typeof change === 'function' ? change(state): action),
});
const Counter = ({step}) => {
const [count, setCount] = React.useReducer(reducerFn, 0);
const handleIncrement = () => {
return setCount(curState => ({count: currentState + step}));
};
// Jsx stuff
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment