Skip to content

What is the best practice for changing a child state in NavigationStack? #3208

Answered by mbrandonw
MorningStarJ asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @MorningStarJ, your approaches are basically correct, though maybe a little verbose. Here is another approach:

guard let bIndex = state.path.firstIndex(where: { $0.is(\.b) })
else {
  return
}

state.path[bIndex].modify(\.b) {
  // Mutate $0
}

There is(casePath) function allows you to easily check if an enum matches a case, and it comes from our CasePaths library. And you can fetch the positional index rather than ID so that you can subscript directly into path. And then you can use modify with a case path to mutate the data inside the path.

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@MorningStarJ
Comment options

@MorningStarJ
Comment options

@mbrandonw
Comment options

@MorningStarJ
Comment options

@Young-Li-wxx
Comment options

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