Skip to content

Commit db3ed65

Browse files
authored
Update SharingState.md
Remove outdated gotcha.
1 parent cc52e99 commit db3ed65

File tree

1 file changed

+0
-59
lines changed

1 file changed

+0
-59
lines changed

Sources/ComposableArchitecture/Documentation.docc/Articles/SharingState.md

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,65 +1051,6 @@ extension AppState: Codable {
10511051
}
10521052
```
10531053

1054-
#### Previews
1055-
1056-
When a preview is run in an app target, the entry point is also created. This means if your entry
1057-
point looks something like this:
1058-
1059-
```swift
1060-
@main
1061-
struct MainApp: App {
1062-
let store = Store()
1063-
1064-
var body: some Scene {
1065-
1066-
}
1067-
}
1068-
```
1069-
1070-
then a store will be created each time you run your preview. This can be problematic with `@Shared`
1071-
and persistence strategies because the first access of a `@Shared` property will use the default
1072-
value provided, and that will cause `@Shared`'s created later to ignore the default. That will mean
1073-
you cannot override shared state in previews.
1074-
1075-
The fix is to delay creation of the store until the entry point's `body` is executed. Further, it
1076-
can be a good idea to also not run the `body` when in tests because that can also interfere with
1077-
tests (as documented in <doc:TestingTCA#Testing-gotchas>). Here is one way this can be accomplished:
1078-
1079-
```swift
1080-
import ComposableArchitecture
1081-
import SwiftUI
1082-
1083-
@main
1084-
struct MainApp: App {
1085-
@MainActor
1086-
static let store = Store()
1087-
1088-
var body: some Scene {
1089-
WindowGroup {
1090-
if isTesting {
1091-
// NB: Don't run application in tests to avoid interference
1092-
// between the app and the test.
1093-
EmptyView()
1094-
} else {
1095-
AppView(store: Self.store)
1096-
}
1097-
}
1098-
}
1099-
}
1100-
```
1101-
1102-
Alternatively you can take an extra step to override shared state in your previews:
1103-
1104-
```swift
1105-
#Preview {
1106-
@Shared(.appStorage("isOn")) var isOn = true
1107-
isOn = true
1108-
}
1109-
```
1110-
1111-
The second assignment of `isOn` will guarantee that it holds a value of `true`.
1112-
11131054
#### Tests
11141055

11151056
While shared properties are compatible with the Composable Architecture's testing tools, assertions

0 commit comments

Comments
 (0)