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
It is important the the **cubit** instance is wrapped inside **@ObservedObject** property wrapper because the **Cubit** class is monitoring the changes of the **state** property via **@PublishedSubject** property wrapper.
85
-
86
54
Since our **State**generic state is **Int** we can directly access to an integer getter **state** and display in in the text.
87
55
88
-
If you want to wrap your **view** in your **body**in a **BlocView**it is done like this:
56
+
Next, inside your **body**property create a **BlocView**with some content:
89
57
90
58
```swift
91
59
var body: some View {
92
-
BlocView(builder: { (state) in
60
+
BlocView(builder: { (cubit) in
93
61
VStack {
94
62
Button(action: {
95
-
self.cubit.increment()
63
+
cubit.increment()
96
64
}, label: {
97
65
Text("Increment")
98
66
})
99
67
Button(action: {
100
-
self.cubit.decrement()
68
+
cubit.decrement()
101
69
}, label: {
102
70
Text("Decrement")
103
71
})
104
-
Text("Count: \(state)")
72
+
Text("Count: \(cubit.state)")
105
73
}
106
-
}, cubit: cubit)
74
+
}, cubit: CounterCubit())
107
75
}
108
76
```
109
77
110
-
In this case you may use **state** directly from a **builder** in order to redruce a getter call from **cubit**. Moreover the benefit of this approach will let to use your **cubit** instance as an **@EnvironmentObject** so every child **view** inside your **builder** function will get the instance of **cubit** without a need "to drill" through the whole view tree.
78
+
In this case you may use **cubit** directly from a **builder**. Moreover this approach will let to use your **cubit** instance as an **@EnvironmentObject** so every child **view** inside your **builder** function will get the instance of **cubit** without a need "to drill" through the whole view tree.
111
79
112
80
### Bloc
113
81
@@ -120,6 +88,8 @@ The difference between **Cubit** and **Bloc** although the **Bloc** is a child c
120
88
As a simple example the **CounterBloc**
121
89
122
90
```swift
91
+
importSwiftBloc
92
+
123
93
enumCounterEvent {
124
94
caseincrement
125
95
casedecrement
@@ -173,33 +143,33 @@ Now let's see what is the view looks like:
Again it is important the the **bloc** instance is wrapped inside **@ObservedObject** property wrapper because the**Bloc** class is monitoring the changes of the **event** property via **@PublishedSubject** property wrapper. This **bloc** instance is set by default as **@EnvironmentObject** and will be available for all child views if needed.
217
+
The**Bloc** class is monitoring the changes of the **event** property via **@PublishedSubject** property wrapper. This **bloc** instance is set by default as **@EnvironmentObject** and will be available for all child views if needed.
0 commit comments