You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+30-8Lines changed: 30 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1307,33 +1307,52 @@ Note that other atoms that depend on scoped atoms will be in a shared state and
1307
1307
1308
1308
#### Override Atoms
1309
1309
1310
-
Overriding an atom in [AtomRoot](#atomroot) or [AtomScope](#atomscope) overwrites its state when used in the descendant views, which is useful for dependency injection or swapping state in a particular view.
1310
+
You can override atoms in [AtomRoot](#atomroot) or [AtomScope](#atomscope) to overwirete the atom states for dependency injection or faking state in particular view, which is useful especially for testing.
1311
+
1312
+
Overriding in `AtomRoot` return the given value instead of the actual atom value no matter where the overridden atom is used in its descendant views.
1311
1313
1312
1314
```swift
1315
+
// Overrides the CounterAtom value to be `456` in anywhere in the ancestor.
1316
+
AtomRoot {
1317
+
RootView()
1318
+
}
1319
+
.override(CounterAtom()) { _in
1320
+
456
1321
+
}
1322
+
```
1323
+
1324
+
On the other hand, overriding with `AtomScope` behaves similar to overriding in `AtomRoot`, but the atoms used in other scopes nested in the descendants are not overridden.
1325
+
1326
+
```swift
1327
+
// Overrides the CounterAtom value to be `456` only for this scope.
1313
1328
AtomScope {
1314
1329
CountDisplay()
1330
+
1331
+
// CounterAtom is not overridden in this scope.
1332
+
AtomScope {
1333
+
CountDisplay()
1334
+
}
1315
1335
}
1316
-
.override(CounterAtom()) { _in
1317
-
456// Overrides the count to be `456` only for this scope.
1336
+
.scopedOverride(CounterAtom()) { _in
1337
+
456
1318
1338
}
1319
1339
```
1320
1340
1321
-
Note that when multiple `AtomScope`s are nested, it doesn't inherit the overrides of its ancestor scopes.
1322
-
In this case, you can explicitly inherit overrides from the parent scope by passing a `@ViewContext` context that has gotten in the parent scope.
1341
+
If you want to inherit the overridden atom from the parent scope, you can explicitly pass `@ViewContext` context that has gotten in the parent scope. Then, the new scope completely inherits the parent scope's context.
1323
1342
1324
1343
```swift
1325
1344
@ViewContext
1326
1345
var context
1327
1346
1328
1347
var body: some {
1329
-
// Inherites the nearest ancester scope's overrides.
1348
+
// Inherites the parent scope's overrides.
1330
1349
AtomScope(inheriting: context) {
1331
1350
CountDisplay()
1332
1351
}
1333
1352
}
1334
1353
```
1335
1354
1336
-
Note also that overridden atoms will automatically be scoped to the `AtomScope`, but other atoms that depend on them will be in a shared state and must be given `Scoped` attribute (See also: [Scoped Atoms](#scoped-atoms)) in order to scope them as well.
1355
+
Note that overridden atoms in `AtomScope`automatically be scoped, but other atoms that depend on them will be in a shared state and must be given `Scoped` attribute (See also: [Scoped Atoms](#scoped-atoms)) in order to avoid it from being shared across out of scope.
1337
1356
1338
1357
See [Testing](#testing) section for details on dependency injection on unit tests.
1339
1358
@@ -1501,7 +1520,8 @@ digraph {
1501
1520
1502
1521
#### Preview
1503
1522
1504
-
Even in SwiftUI previews, the view must have an `AtomRoot` somewhere in the ancestor. However, since This library offers the new solution for dependency injection, you don't need to do painful DI each time you create previews anymore. You can to override the atoms that you really want to inject substitutions.
1523
+
Even in SwiftUI previews, the view must have an `AtomRoot` somewhere in the ancestor.
1524
+
To inject dependencies so that display a static preview, define the dependencies as atoms and override them.
0 commit comments