Possible to translate a useReducer to Zustand?
See original GitHub issueI 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:
- Created 3 years ago
- Comments:10 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If you wanted to stick with a reducer:
If you wanted to do it more traditional zustand:
Closing this as stale. Please file a new issue with a repro. We would like to solve issues if there are any with Gatsby.