Skip to content

Commit 2a1bccd

Browse files
author
Krasimir Tsonev
committed
Adding proper images for one-direction data flow section
1 parent 68a10f3 commit 2a1bccd

File tree

4 files changed

+3
-37
lines changed

4 files changed

+3
-37
lines changed

assets/images.psd

246 KB
Binary file not shown.

patterns/one-direction-data-flow/README.md

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,7 @@ function App() {
6969

7070
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:
7171

72-
```
73-
User's input
74-
|
75-
Switcher -------> Store
76-
```
72+
![one-direction data flow](./one-direction-1.jpg)
7773

7874
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.
7975

@@ -92,17 +88,7 @@ constructor(props) {
9288
9389
Our workflow changes to the following:
9490
95-
```
96-
User's input
97-
|
98-
Switcher <-------> Store
99-
^ |
100-
| |
101-
| |
102-
| v
103-
Service communicating
104-
with our backend
105-
```
91+
![one-direction data flow](./one-direction-2.jpg)
10692
10793
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.
10894
@@ -161,24 +147,4 @@ function Switcher({ value, onChange }) {
161147
162148
## Final thoughts
163149
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.
Loading
Loading

0 commit comments

Comments
 (0)