Ad hoc bindings
The simplest tool is to create a dedicated action that changes a piece of state.1
Define state property
2
Define corresponding action
3
Handle the action
4
Derive binding in view
First, hold the store in a bindable way:
If targeting iOS 16 or earlier, use
@Perception.Bindable instead of @Bindable.5
Create the binding
Binding actions and reducers
For screens with many controls, creating individual actions for each binding can be tedious. The library providesBindableAction and BindingReducer to eliminate boilerplate.
The problem
Consider a settings screen with many editable fields:The solution
1
Conform action to BindableAction
2
Add BindingReducer
3
Make store bindable in view
4
Derive bindings with $ syntax
Observing specific bindings
You can layer additional functionality over bindings by pattern matching:Pattern matching in reducer
Using onChange
Alternatively, useonChange on the BindingReducer:
Testing bindings
Binding actions can be tested just like regular actions. Instead of sending a specific action like.displayNameChanged("Blob"), you send a BindingAction:
Advanced: Custom bindings
You can create custom bindings that perform additional logic:Best practices
Use BindingReducer for many bindings
When you have 3+ bindable fields, use
BindingReducer instead of ad hoc bindingsValidate with onChange
Use
onChange to add validation or side effects for specific fieldsTest binding actions
Always test binding actions to ensure proper state mutations
Keep logic in reducer
Don’t put business logic in custom binding closures; keep it in the reducer
Common patterns
Conditional bindings
Sometimes you only want to update state when certain conditions are met:Derived bindings
You can derive bindings from computed properties:Debounced bindings
For expensive operations, debounce binding changes:Troubleshooting
Binding not updating the view
Binding not updating the view
Make sure:
- Your store is marked with
@Bindable(or@Perception.Bindablefor iOS 16) - Your
Stateis marked with@ObservableState - You’re using
$storesyntax to derive bindings
Compilation error with @Bindable
Compilation error with @Bindable
If targeting iOS 16 or earlier, use the backported version:
Action not being sent
Action not being sent
Verify that:
BindingReducer()is in your reducer’s body- Your action enum conforms to
BindableAction - You have a
case binding(BindingAction<State>)in your action enum