Skip to content

Merge properties #50

Description

@mikaelkarlsson-se

Hello there 👋
First of, thanks for a nice package! 🤝

I have a situation where I would like to mix regular state properties and properties returned by this package within a nested object. What I would like is this:

somePropertyAtTheFirstLevelOfTheState: "...",
computed: {
  somePropertyThatIsARegularStateProperty: "...",
  somePropertyThatIsComputedByZustandComputed: "..."
} 

Some of my computed properties are based on other store states, so I have subscriptions that update the state when needed. Conceptually they are all computed so of course I want to have them at the same place.

I've realized that if I create my computed state like this it will not merge the properties within computed but replace the whole object:

const computed = createComputed((state) => ({
  computed: { somePropertyThatIsComputedByZustandComputed: state.someThing * 2 }
}))

However I can simply spread computed to make it work:

const computed = createComputed((state) => ({
  computed: {
    ...state.computed,
    somePropertyThatIsComputedByZustandComputed: state.someThing * 2
  }
}))

This feels a bit weird though and so my question is simply if this is a bad idea or not? I'm thinking e.g that there might create a dependency to state.computed and cause a loop? ♻

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions