Difference between @State
and @Binding
3 min readMar 26, 2024
Understanding the difference between @State
and @Binding
in SwiftUI is crucial for managing data flow in your apps effectively. Here's a breakdown of each and how they differ:
@State
- Purpose:
@State
is used to declare a source of truth for data within a single view. It's meant for simple data types that are local to the view. When the@State
variable changes, SwiftUI automatically rerenders the part of the view that depends on this variable, allowing your UI to update in response to data changes. - Ownership: A
@State
property is owned by the view. This means the data is stored directly within the view, and the view controls the lifecycle of this data. - Privacy: Typically,
@State
variables are declared asprivate
because they're intended to be accessed and modified only by the view that owns them. This encapsulation ensures that the view's state cannot be inadvertently changed from outside the view. - Usage: Use
@State
for temporary storage of app data in a view. It's suitable for simple UI state management, like toggling a button, entering text in a field, etc.
@Binding
- Purpose:
@Binding
is used to create a two-way connection between a view and its parent or another view. It allows a view to read and write a state owned by another view without owning that state directly. When the data changes, both the original owner of the state and any views using a@Binding
to that state are updated. - Ownership: A
@Binding
property does not own the data it refers to. Instead, it acts as a reference or a pointer to the state owned by another view. This approach allows multiple views to interact with the same piece of data. - Privacy:
@Binding
variables are typically passed into a view's initializer or as part of a view's configuration. They're meant to be set up by the parent or another view that owns the original source of truth (@State
or other property wrappers like@ObservedObject
,@EnvironmentObject
, etc.). - Usage: Use
@Binding
when you need to modify a state from a different view than the one that owns it. This is common in custom components or child views that need to update shared data.
Key Differences
- Ownership:
@State
is for state owned by the view itself, while@Binding
is for accessing and mutating state owned by another view. - Control:
@State
gives a view control over its own data, ensuring that it can manage its UI state internally.@Binding
allows a view to manipulate data owned elsewhere, facilitating communication between views. - Use Case: You use
@State
to manage a view's internal state and@Binding
to allow a child or another view to update the state that's owned by a different view.
Example to Illustrate
struct ParentView: View {
@State private var toggleStatus = false // Owned by ParentView
var body: some View {
ChildView(toggleStatus: $toggleStatus) // Passing a binding to ChildView
}
}
struct ChildView: View {
@Binding var toggleStatus: Bool // Reference to the state in ParentView
var body: some View {
Toggle("Enable feature", isOn: $toggleStatus)
}
}
In this example, ParentView
owns the toggleStatus
state. ChildView
needs to update this state based on user interaction with a Toggle
. By passing a @Binding
to ChildView
, both views stay in sync with the current toggleStatus
.
This difference between @State
and @Binding
is foundational to managing state in SwiftUI applications, ensuring data consistency and reactive UI updates across your app.