Skip to content

Instantly share code, notes, and snippets.

@LoganBarnett
Created February 9, 2024 04:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LoganBarnett/f4438ca3d11f31654907024d43bb4200 to your computer and use it in GitHub Desktop.
Save LoganBarnett/f4438ca3d11f31654907024d43bb4200 to your computer and use it in GitHub Desktop.
const muhReducer = (state, action) => {
switch(action.type) {
case 'FROBNICATE':
return {
frobnicateCount: state.frobnicateCount + 1,
...state,
}
default:
return state
}
}
// A button
<MuhButton onClick={e => dispatch({ type: 'FROBNICATE' }) } >Frobnicate</MuhButton>
@LoganBarnett
Copy link
Author

A nested reducer:

const catReducer = (cats, action) => {
  switch(action.type) {
    case 'CAT_BLEND':
      return {
        catLives: cats.catLives - 1,
        ...cats,
      }
    default:
      return cats;
  }
}

const muhReducer = (state, action) => {
  switch(action.type) {
    case 'FROBNICATE':
      return {
        frobnicateCount: state.frobnicateCount + 1,
        ...state,
      }
    default:
      return {
        cats: catReducer(state.cats, action),
        ...state,
      }
  }
}

// A button

<MuhButton onClick={e => dispatch({ type: 'FROBNICATE' }) } >Frobnicate</MuhButton>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment