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
We assume that the reader is already aware of Redux. But just to refresh, according to redux.js.org:
3
+
We assume that the reader is already aware of Redux. But just to recap, according to redux.js.org:
4
4
5
5
> Redux is a predictable state container for JavaScript apps. It helps you write applications that behave consistently, run in different environments (client, server, and native), and are easy to test. On top of that, it provides a great developer experience, such as live code editing combined with a time traveling debugger.
6
6
7
-
In a nutshell, redux is a state container. That is it will contain all our runtime application state or data.
7
+
In a nutshell, Redux is a state container. That means, it will contain all our runtime application state or data.
8
8
9
-
Redux essentially has a three parts:
10
-
- Store - Store contains a global state for the entire app. It is basically the manager for the application state.
11
-
- Actions - These are like the commands you pass to store along with some data to do some modifications to the stored state.
12
-
- Reducers - Reducers are basically functions that the store calls whenever a action arrives. The reducers are the ones which determines what the new state will be based on the actions and the action payload it receives.
9
+
Redux is essentially made up of three parts:
10
+
- Store - The store contains a global state for the entire app. It is basically the manager of the application state.
11
+
- Actions - These are the commands you pass to the store along with some data to modify the stored state.
12
+
- Reducers - Reducers are functions that the store calls whenever an action arrives. The reducers determine what the new state will be based on the action and the action payload they receive.
13
13
14
14
### But we already have React's state
15
15
16
16
Both Redux and React's state are used to manage the state in the application. But, both Redux and React's state are very different in the way they work and it is good to know when to use what.
17
17
18
-
React state is stored locally within a component. When it needs to be shared with other components, it is passed down through the props. Hence, this means all the components which need the state data need to be the child of the component holding the value.
19
-
But in case of Redux, state is stored globally in the Redux store. Components subscribe to the store to get access to the value. This centralizes all data but makes it very easy for a component to get the state it needs, without surrounding components knowing of its needs.
18
+
React state is stored locally within a component. When it needs to be shared with other components, it is passed down through the props. This means that all the components which need the state data need to be children of the component holding the value.
19
+
But in case of Redux, the state is stored globally in the Redux store. Components subscribe to the store to get access to the value. This centralizes all data and makes it very easy for a component to get the state it needs, without surrounding components being affected.
20
20
21
-
So, this means that Redux is good and we should just use it for all our app state management.
21
+
So, does this means that Redux is great and we should use it for all our app state management?
22
22
23
-
**NO!**
23
+
**NO!**
24
24
25
-
While redux is helpful in some cases, it will create unnecessary indirections for simpler and trivial use cases.
26
-
Consider that we have a text input. And since we are using redux, we decide to use redux to store all the changes in the text field in redux. In redux for changing the state on text input, we will need to create an Action, write a reducer and then subscribe our component to the store so that it re renders on every state change. This is bad! Why so much complication?
25
+
While Redux is helpful in some cases, it will create unnecessary indirections for simpler and trivial use cases.
27
26
28
-
*Dan Abramov* - The creator of redux says you might actually not need redux unless you have a plan to benefit from this additional indirection. In his blog at https://medium.com/@dan_abramov/you-might-not-need-redux-be46360cf367, he clearly states that since redux introduces an additional indirection to managing the state of the application, we should use it only if it benefits us.
27
+
Consider that we have a text input. And since we are using Redux, we decide to use Redux to store all the changes in the text field in the Redux store. In Redux, for changing the state on text input, we will need to create an Action, write a reducer and then subscribe our component to the store so that it re-renders on every state change. This is bad! Why do we need to complicate things so much?
29
28
30
-
Now, lets see when we should use redux and when React's state is good enough.
29
+
*Dan Abramov* - The creator of Redux says you might actually not need Redux unless you have a plan to benefit from this additional indirection. In his blog at https://medium.com/@dan_abramov/you-might-not-need-redux-be46360cf367, he clearly states that since Redux introduces an additional indirection to managing the state of the application, we should use it only if it benefits us.
31
30
32
-
One way to do this is based on Duration of the data we want to store.
33
-
As, *Tyler Hoffman* explains in his blog post https://spin.atomicobject.com/2017/06/07/react-state-vs-redux-state/,
31
+
Now, let's see when we should use Redux and when React's state is good enough.
32
+
33
+
One way to do this is based on the duration the data has to be stored.
34
+
As *Tyler Hoffman* explains in his blog post: https://spin.atomicobject.com/2017/06/07/react-state-vs-redux-state/,
34
35
35
36
Different pieces of state are persisted for different amounts of time. We can categorize data into:
36
37
37
38
-**Short term**: data that will change rapidly in your app
38
39
-**Medium term**: data that will likely persist for a while in your app
39
-
-**Long term**: data that should last between multiple app launch.
40
+
-**Long term**: data that should last between multiple app launches.
40
41
41
42
#### Short term data
42
-
Short term data is something that changes very quickly and is needed to be stored only for small amount of time. For example, this includes the characters that the user types in a text field. This data is not needed once the user submits the form.
43
-
Also we will not need to mostly transfer this type of data to any other independent component. Such type of data clearly fits the use case for React's state.
43
+
Short term data is data that changes very quickly and is needed to be stored only for small amount of time. For example, this includes the characters that a user types in a text field. This data is not needed once the user submits the form.
44
+
Also, we will most likely not need to transfer this type of data to any other independent component. Such type of data clearly fits the use case for React's state.
44
45
45
46
46
47
#### Medium term data
47
-
Medium term data needs to stick around while the user navigates the app. This could be data loaded from an API, or any changes that needs to be persisted up until a page refresh. This can also contain data that needs to be used by a component that is completely unrelated to the component that produced the data. As an example, consider that one of the component makes an API call on a button click to update user's profile details. The data returned from the server needs to be stored and will be used by a completely unrelated profile screen. Now, if the data is was stored in some global location. It would be far easier to access this data. Such type of use cases clearly fits Redux.
48
+
Medium term data needs to stick around while the user navigates through the app. This could be data loaded from an API or any changes that need to be persisted until a page refresh. This can also contain data that needs to be used by a component that is completely unrelated to the component that produced the data. As an example, consider that one of the components makes an API call on a button click to update the user's profile details. The data returned from the server needs to be stored and will be used by a completely unrelated profile screen. Now, if the data is stored in some global location, it will be far easier to access this data. Such type of use cases clearly fits Redux.
48
49
49
50
#### Long term data
50
-
This is the data that should be persisted between refreshes of the page or when the user closes and reopens the app. Since the Redux store is created on app launch, this type of data should be stored somewhere else, for example: Async Storage in the case of React Native.
51
+
This is the data that should be persisted between page refreshes or when the user closes and reopens the app. Since the Redux store is created on app launch, this type of data should be stored somewhere else, for example Async Storage in the case of React Native.
But if you look at the app now, there is no way for us to get the character count from the TextArea component and use it as the text for character count text view.
86
+
But if you look at the app now, there is no way for us to get the character count from the TextArea component and use it as the text for the character count text view.
86
87
To do this we will need to **move the state present inside the TextArea component and place it in the Home component**.
87
-
This is because all the components that need access to a state have to be the child of the component holding the state.
88
+
This is because all the components that need access to a state have to be children of the component holding the state.
88
89
89
90
So we modify our components as follows:
90
91
>app/components/TextArea/TextArea.component.js
@@ -159,27 +160,28 @@ The character count should now update whenever you enter text on the text field.
159
160
<br/>
160
161
161
162
162
-
Hence, here by moving the state from the child component to parent, we were able to access it in multiple child components.
163
-
Thus, to provide access to the data that needs to be accessed by multiple components, we need to have the state in the enclosing parent component. Thus following this principle, if we keep on moving the state to the parent component, we will end up with a state present in the top most level component.
163
+
By moving the state from the child component to the parent, we were able to access it in multiple children components.
164
+
165
+
Therefore, to provide access to the data that needs to be accessed by multiple components, we need to have the state in the enclosing parent component. Following this principle, if we keep on moving the state to the parent component, we will end up with the state in the topmost level component.
164
166
165
-
Redux builds on top of similar principles. It keeps a global store to which the components which need access to the data can subscribe to. Additionally, it provides a mechanism by which these components can re-render whenever the data in the store changes.
167
+
Redux builds on top of similar principles. It keeps a global store to which the components which need access to the data can subscribe. Additionally, it provides a mechanism by which these components can re-render whenever the data in the store changes.
166
168
167
-
Now, since we are clear why redux is helpful. Lets setup redux for our app.
169
+
Now, since we understand how Redux is helpful, let's setup Redux for our app.
0 commit comments