Skip to content

How to pass a subset of state to a substore #1125

Answered by tgrapperon
ivangodfather asked this question in Q&A
Discussion options

You must be logged in to vote

Hello @ivangodfather!
As often, there are several ways to achieve this.
The most common is to rely on a computed property that is updated on "get" and reinjected on "set". So, with your current model:

extension AppState {
  var updateUser: UpdateUserState {
    get {
      UpdateUserState(username: user.username)
    }
    set {
      self.user.username = newValue.username
    }
  }
}

This is more foolproof than having to account for each possible username modification.
Using this setup, a whole new UpdateUserState is delivered each time. It furthermore frees one stored field from your AppState which is always a good thing (faster automatic equatability, smaller footprint on the stack, etc.)

Replies: 1 comment 9 replies

Comment options

You must be logged in to vote
9 replies
@ivangodfather
Comment options

@ivangodfather
Comment options

@tgrapperon
Comment options

@ivangodfather
Comment options

@tgrapperon
Comment options

Answer selected by ivangodfather
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants