diff --git a/Sources/ComposableArchitecture/Documentation.docc/Articles/Reducers.md b/Sources/ComposableArchitecture/Documentation.docc/Articles/Reducers.md index 0fb133a6173e..9231efdfc33f 100644 --- a/Sources/ComposableArchitecture/Documentation.docc/Articles/Reducers.md +++ b/Sources/ComposableArchitecture/Documentation.docc/Articles/Reducers.md @@ -22,6 +22,7 @@ more concise and more powerful. * [Gotchas](#Gotchas) * [Autocomplete](#Autocomplete) * [Circular reference errors](#Circular-reference-errors) + * [#Preview and enum reducers](#Preview-and-enum-reducers) * [CI build failures](#CI-build-failures) ## Conforming to the Reducer protocol @@ -449,6 +450,69 @@ This unfortunately does not work. It is a move the extension to a separate file, or move the code from the extension to be directly inside the `State` type. +#### #Preview and enum reducers + +The `#Preview` macro is not capable of seeing the expansion of any macros since it is a macro +itself. This means that when using destination and path reducers (see + above) you cannot construct the cases of the state +enum inside `#Preview`: + +```swift +#Preview { + FeatureView( + store: Store( + initialState: Feature.State( + destination: .edit(EditFeature.State()) // 🛑 + ) + ) { + Feature() + } + ) +} +``` + +The `.edit` case is not usable from within `#Preview` since it is generated by the ``Reducer()`` +macro. + +The workaround is to move the view to a helper that be compiled outside of a macro, and then use it +inside the macro: + +```swift +#Preview { + preview +} +private var preview: some View { + FeatureView( + store: Store( + initialState: Feature.State( + destination: .edit(EditFeature.State()) + ) + ) { + Feature() + } + ) +} +``` + +You can use a computed property, free function, or even a dedicated view if you want. You can also +use the old, non-macro style of previews by using a `PreviewProvider`: + +```swift +struct Feature_Previews: PreviewProvider { + static var previews: some View { + FeatureView( + store: Store( + initialState: Feature.State( + destination: .edit(EditFeature.State()) + ) + ) { + Feature() + } + ) + } +} +``` + #### CI build failures When testing your code on an external CI server you may run into errors such as the following: