Updating State - "Best Practice" Question
See original GitHub issueHi, have a quesioin regarding reusing state update logic. I want my state update logic to be in the store only, where the state + actions are, and i’m looking for a “best practice” of reusing a function that updates part of the state, in Mobx State Tree, for example, the store is built of smaller parts that i can reuse in different places. is it possible to do it in zustand?
MST ex:
// reusable logic part
const Todo = types
.model("Todo", {
title: types.string,
done: false
})
.actions(self => ({
toggle() {
self.done = !self.done
}
}))
// using the reusable update logic
const Store = types.model("Store", {
todos: types.array(Todo)
})
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (6 by maintainers)
Top Results From Across the Web
5 Best Practices for Handling State Structure in React
5. Avoid Deeply Nested State. Deeply hierarchical state is not very easy to update, hence it is a best practice to make the...
Read more >React Hooks cheat sheet: Best practices with examples
Included in this React Hooks cheat sheet are best practices related to ... Updating a state variable is as simple as invoking the...
Read more >React State Management best practices (aka no Redux)
Since there is no prop drilling, we don't have to update every component like when lifting a state without context.
Read more >Top 6 Best Practices For State Management with React - Blog
#2 - State management is how you update your page after creating/updating data, without a refresh. The problem. Say you're writing a to-do...
Read more >Best practice for updating individual state properties with ...
My question, as noted in the code, is that I'm not sure where/how the logic to merge the updated value into the state...
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
zustand is basically unopinionated, but it’s easy if you have actions right under the state because the default behavior of
set
is shallow merging.Now, if I’d try something you would expect, it’d be:
…but, I don’t think this is something you really expect.
@dai-shi the problem that rises from putting actions in a store is a very long file sometimes that is not easy to ready, even though putting controllers in separate files with custom hooks can be cumbersome, it still guarantees readability.