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: patterns/one-direction-data-flow/README.md
+3-37Lines changed: 3 additions & 37 deletions
Original file line number
Diff line number
Diff line change
@@ -69,11 +69,7 @@ function App() {
69
69
70
70
Our `Store` object is a [singleton](https://addyosmani.com/resources/essentialjsdesignpatterns/book/#singletonpatternjavascript) where we have helpers for setting and getting the value of the `_flag` property. By passing the getter to the component we are able to update the data externally. More or less our application workflow looks like that:
71
71
72
-
```
73
-
User's input
74
-
|
75
-
Switcher -------> Store
76
-
```
72
+

77
73
78
74
Let's assume that we are saving the flag value to a back-end service via the `Store`. When the user comes back we have to set a proper initial state. If the user left the flag as `true` we have to show *"lights on"* and not the default *"lights off"*. Now it gets tricky because we have the data knowledge in two places. The UI and the `Store` have their own states. We have to communicate in both directions from the store to the switcher and from the switcher to the store.
79
75
@@ -92,17 +88,7 @@ constructor(props) {
92
88
93
89
Our workflow changes to the following:
94
90
95
-
```
96
-
User's input
97
-
|
98
-
Switcher <-------> Store
99
-
^ |
100
-
| |
101
-
| |
102
-
| v
103
-
Service communicating
104
-
with our backend
105
-
```
91
+

106
92
107
93
All this leads to managing two states instead of one. What if the `Store` changes its value based on other actions in the system. We have to propagate that change to the `Switcher` and we increase the complexity of our app.
108
94
@@ -161,24 +147,4 @@ function Switcher({ value, onChange }) {
161
147
162
148
## Final thoughts
163
149
164
-
The benefit that comes with this pattern is that our components become dummy representation of the store's data. It is really easy to think about the React components as views (renderers). We write our application in a declarative way and deal with the complexity in only one place.
165
-
166
-
The diagram of the application changes to:
167
-
168
-
```
169
-
Service communicating
170
-
with our backend
171
-
^
172
-
|
173
-
v
174
-
Store <-----
175
-
| |
176
-
v |
177
-
Switcher ---->
178
-
^
179
-
|
180
-
|
181
-
User input
182
-
```
183
-
184
-
As we can see the data flows in only one direction and there is no need to sync two (or more) parts of our app. One-way direction data flow is not only about React based apps. It makes sense to any type of application which is heavily depending on UI updates.
150
+
The benefit that comes with this pattern is that our components become dummy representation of the store's data. There is only one source of truth and this makes the development easier.
0 commit comments