question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Possible to translate a useReducer to Zustand?

See original GitHub issue

I have a reducer:

const reducer = (state, action) => {

  switch (action.change) {
    case 'ROOT' :
      return {
        ...state,
        root: action.value
      }
    case 'QUALITY' :
      return {
        ...state,
        quality: action.value
      }
    case 'EXTENSION' :
      return {
        ...state,
        extension: action.value
      }
    case 'CHORD' :
      return {
        ...state,
        quality: action.value.split(`-`)[0],
        extension: action.value.split(`-`)[1]
      }
    case 'RESET' :
      return { 
        ...state,
        root: '',
        quality: '',
        extension: ''
      }
    default :
      return state
  }
}

…which is used in a react component utilizing the useReducer hook, like so:

const [{ root, quality, extension }, dispatch] = useReducer(
    reducer,
    {
      root: ( props.root || '' ),
      quality: ( props.quality || '' ),
      extension: ( props.extension || '' )
    }
  )

  const handleRootChange = event =>
    dispatch({ change: 'ROOT', value: event.target.value })

  const handleQualityChange = event =>
    dispatch({ change: 'QUALITY', value: event.target.value })

  const handleExtensionChange = event =>
    dispatch({ change: 'EXTENSION', value: event.target.value })

  const handleChordChange = event => {
    dispatch({ change: 'CHORD', value: event.target.value })
  }

  const handleReset = event =>
    dispatch({ change: 'RESET'})

  const notes = `${root}-${quality}-${extension}`

…soooo, can something like this be achieved using Zustand in a simper way?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
timkindbergcommented, Aug 18, 2020

If you wanted to stick with a reducer:

// Create Store
const useStore = create(set => ({
  root: '',
  quality: '',
  extension: ''
  dispatch: args => set(state => reducer(state, args)),
}))

// Usage in Component
const dispatch = useStore(state => state.dispatch)
dispatch({ change: 'ROOT', value: event.target.value })

If you wanted to do it more traditional zustand:

const useStore = create(set => ({ 
  root: '',
  quality: '',
  extension: '',
  actions: {
    changeRoot: root => set({ root }),
    changeQuality: quality => set({ quality }),
    changeExt: extension => set({ extension }),
    changeChord: chord => set({
        quality: chord.split(`-`)[0],
        extension: chord.split(`-`)[1]
    }),
    reset: () => set({ 
      root: '',
      quality: '',
      extension: ''
    })
  }
}))

// Usage
const root = useStore(s => s.root)
const actions = useStore(s => s.actions)
actions.changeRoot(...)
0reactions
dai-shicommented, Sep 15, 2020

Closing this as stale. Please file a new issue with a repro. We would like to solve issues if there are any with Gatsby.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Possible to translate a useReducer to Zustand? #158 - GitHub
I have a reducer: const reducer = (state, action) => { switch (action.change) { case 'ROOT' : return { ...state, root: action.value }...
Read more >
Working with Zustand | TkDodo's blog
Let's dive into some tips for working with Zustand - one of my favourite client state management libraries in React.
Read more >
Replace useReducer with Zustand state manager : r/reactjs
I know that it is possible to replace useReducers with Zustand, I just don't get it! And I love Redux, but Zustand is...
Read more >
How to Manage State in a React App – With Hooks, Redux ...
To sum it up, we just need: A reducer, that is the function that will consolidate all possible state changes. A dispatch function,...
Read more >
The single, most important trick to build robust applications
I hear some of you screaming: "You should have used useReducer/Redux/Zustand/{random pokemon name} instead of those multiple useState!".
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found