Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ViewStore.binding migration examples to guide #3087

Merged
merged 4 commits into from
May 14, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,53 @@ TabView(selection: $store.tab.sending(\.tabChanged)) {
}
```

If the binding depends on more complex business logic, you can define a custom `get`-`set` property
(or subscript, if this logic depends on external state) on the store to incorporate this logic. For
example:

@Row {
@Column {
```swift
// Before

// In the view:
ForEach(Flag.allCases) { flag in
Toggle(
flag.description,
isOn: viewStore.binding(
get: { store.featureFlags.contains(flag) }
send: { store.send(.toggleFeature(flag)) }
)
)
}
```
}
@Column {
```swift
// After

// In the file:
extension StoreOf<Feature> {
subscript(hasFeatureFlag flag: Flag) {
get { featureFlags.contains(flag) }
set { send(.toggleFeature(flag) }
}
}

// In the view:
ForEach(Flag.allCases) { flag in
Toggle(
flag.description,
isOn: $store[hasFeatureFlag: flag]
)
}
```
}
}

> **Tip:** When possible, consider moving complex binding logic into the reducer so that it can be
> more easily tested.

## Computed view state

If you are using the `ViewState` pattern in your application, then you may be computing values
Expand Down