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.

Updating State - "Best Practice" Question

See original GitHub issue

Hi, 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:closed
  • Created 3 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
dai-shicommented, Oct 17, 2020

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:

const createTodo = (set) => ({
  title: '',
  done: false,
  setTitle: (title) => set({ title }),
  toggle: () => set(prev => ({ done: !prev.done }),
})

const createStore = create((set) => ({
  todos: {},
  addTodo: () => {
    const id = nanoid()
    const setTodo = (update) => {
      set(prev => {
        if (typeof update === 'function') {
          update = update(prev.todos[id])
        }
        return { todos: { ...prev.todos, [id]: { ...prev.todos[id], ...update } } }
      })
    }
    const todo = createTodo(setTodo)
    set(prev => ({ todos: { ...prev.todos, [id]: todo }))
  }
})

…but, I don’t think this is something you really expect.

0reactions
feuersteinercommented, Sep 22, 2022

@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.

Read more comments on GitHub >

github_iconTop 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 >

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