In a SwiftUI #Preview, how do you give a component that takes a Binding real, editable state, and what does @Previewable generate?
answer
- bindings need an owner
- .constant ignores writes
- root scope of the preview body
- tagged declarations become view properties
basics
~20 sTag a root-level @State in the #Preview body with @Previewable. The macro generates an embedded view, turns tagged declarations into its properties, and lets you pass $value as a live binding instead of a frozen .constant one.
solid answer
~40 sApple's documentation says `@State` is created on a property of an App, Scene or View, and a `#Preview` closure is none of those. That is why the old choices were `.constant(value)`, which renders but ignores writes, or a hand-written wrapper view. `@Previewable` removes the wrapper: write `@Previewable @State var isLiked = false` at the root of the preview body, and the `#Preview` macro generates an embedded view whose properties are the tagged declarations and whose body is the remaining statements. SwiftUI then manages the value like any view state, so `LikeButton(isLiked: $isLiked)` toggles in an interactive preview. It works for other dynamic properties too, such as `@FocusState`. Using it outside a `#Preview` body is a compile error.
code
swift · 22 linesimport SwiftUI
struct LikeButton: View {
@Binding var isLiked: Bool
var body: some View {
Button {
isLiked.toggle()
} label: {
Label("Like", systemImage: isLiked ? "heart.fill" : "heart")
}
}
}
#Preview("Tappable") {
@Previewable @State var isLiked = false
LikeButton(isLiked: $isLiked)
}
#Preview("Always liked") {
LikeButton(isLiked: .constant(true))
}go deeper
Recall that a component taking a Binding needs state to bind to in its preview, and that @Previewable @State gives it one.
Explain the expansion: the macro generates a view, tagged declarations become its properties, and that is why the binding is live while .constant drops writes.
Choose deliberately between constant, @Previewable and wrapper views, so visual-state previews stay stable and interactive ones stay small and side-effect free.
Set a house convention for component previews, with fixed-state previews for review and one interactive preview each, so a design system's states stay visible and consistent.
## The problem: a binding needs a source of truth Many SwiftUI components do not own their value. A like button, a slider or a text field takes a `Binding`, a reference to state that something above it owns. To preview such a component you need state to bind to, and the obvious first attempt does not do what you want. Apple's documentation for `State` says you create a state value **in an App, Scene, or View** by applying `@State` to a *property* declaration, and that SwiftUI then manages its storage. A `#Preview` closure is none of those: it is a closure that returns a view. So the traditional workarounds were: - **`Binding.constant(_:)`**: pass `.constant(true)`. It compiles and renders, but the binding ignores writes, so tapping the control in the preview does nothing. - **A wrapper view**: write a small `struct LikeButtonPreview: View` that owns `@State` and passes `$isLiked` down. It works, but it is boilerplate for every interactive preview. ## What @Previewable does `@Previewable` is an attached peer macro that removes the boilerplate. According to Apple's documentation: 1. You tag a variable declaration **at root scope** in the `#Preview` body with `@Previewable`, for example `@Previewable @State var isLiked = false`. 2. The `#Preview` macro **generates an embedded SwiftUI view**. 3. Tagged declarations become **properties of that view**, and all remaining statements form the view's body. Because the state is now a real property of a real view, SwiftUI manages it like any other `@State`. `$isLiked` is a live binding, and tapping the button in an interactive preview flips the heart. The documentation describes the macro as letting *dynamic properties* appear inline, not only `@State`. `@FocusState` works the same way. Apple's SwiftData example uses `@Previewable @Query`, a subject covered with SwiftData's SwiftUI integration. ## Rules and limits - It is **an error to use `@Previewable` outside a `#Preview` body**. It cannot mark a property of your own view as preview-only. - Only declarations **at the root** of the closure are lifted onto the generated view; a variable declared inside a nested closure is not. - It does not inject anything into the environment. Child views see the value only through the binding or argument you pass them. - The macro arrived in the June 2024 release (the iOS 18 SDK), but its availability line reads iOS 17, so a target that deploys to iOS 17 can use it. ## Choosing between the three options | Option | Interactive? | Boilerplate | Use it when | |---|---|---|---| | `.constant(value)` | no, writes are ignored | none | you only need to see one fixed state | | `@Previewable @State` | yes | one line | you want to tap, type or drag in the preview | | hand-written wrapper view | yes | a struct | the harness needs logic, several properties or reuse across files | A common pattern is to pair them: a `.constant` preview per visual state ("Liked", "Not liked") plus one `@Previewable` preview for checking the interaction. ## What changes in Xcode 27 Apple's `State` documentation notes that building with Xcode 27 or later turns `@State` into the `State()` macro. The `@Previewable` documentation's own example still writes `@Previewable @State var toggled = true`, so the spelling inside a preview does not change: you tag the state declaration, and the generated view owns it. ## A worked example The `LikeButton` below owns nothing. It toggles whatever binding it is given. The "Tappable" preview uses `@Previewable` so the heart fills when you tap it in the canvas's live mode. The "Always liked" preview pins the liked state with `.constant(true)`, which makes a stable visual check, but tapping it changes nothing. Each of these previews is fast and free of side effects. Nothing loads, and the only state is a `Bool` that the preview owns. Keeping interactive previews this small lets you test the component's interaction without starting the screen around it. ## Key points - `@Previewable` moves a root-level `@State` (or another dynamic property) of a `#Preview` body onto a view that the macro generates, so `$value` is a live binding. - `.constant` is for fixed states. It silently drops writes. - `@Previewable` is legal only inside a `#Preview` body.
- Can @Previewable tag property wrappers other than @State?Yes. Apple's documentation describes it as letting dynamic properties appear inline in previews, so `@FocusState` and similar wrappers that must live on a view work the same way, and SwiftData's own preview example uses `@Previewable @Query`. The declaration must sit at the root of the `#Preview` body, because that is what the macro lifts onto the generated view.
- When is a hand-written wrapper view still the better choice?When the preview harness needs real logic, several coordinated properties or reuse across files. A named wrapper view can hold helper methods, an initializer and its own previews, while `@Previewable` is meant for a line or two of inline state. Under the hood both give SwiftUI a view that owns the state.
A .constant binding is a photo of a light switch: you can see the position, but pressing it does nothing. @Previewable wires the switch to a real circuit owned by the preview, so pressing it changes the light.
- A plain @State in the preview closure is managed exactly like a view property.
- @Previewable can mark a view's own stored properties as preview-only.
- A .constant binding lets the preview toggle its value when you tap the control.
- @Previewable puts the value into the environment for every child view.
- @Previewable needs an iOS 18 deployment target because it shipped with that SDK.