Skip to content

Commit 565a00e

Browse files
kakulguptaa7ul
authored andcommitted
Proofing chapter redux (#27)
1 parent 24b1d6d commit 565a00e

File tree

3 files changed

+74
-73
lines changed

3 files changed

+74
-73
lines changed

9-redux/9.0-intro.md

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,51 @@
11
# Redux - The State Container
22

3-
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:
44

55
> 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.
66
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.
88

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.
1313

1414
### But we already have React's state
1515

1616
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.
1717

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.
2020

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?
2222

23-
**NO !**
23+
**NO!**
2424

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.
2726

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?
2928

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.
3130

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/,
3435

3536
Different pieces of state are persisted for different amounts of time. We can categorize data into:
3637

3738
- **Short term**: data that will change rapidly in your app
3839
- **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.
4041

4142
#### 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.
4445

4546

4647
#### 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.
4849

4950
#### 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.

9-redux/9.1-redux-setup.md

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Setting up Redux for React Native
22

3-
### Why redux ?
3+
### Why Redux?
44

5-
Lets say in our note taker app. We need to add the character count and a save/sync button at bottom.
6-
First lets create the UI component for it.
5+
Let's say in our note-taker app, we need to add the character count and a save/sync button at the bottom of the screen.
6+
First, let's create the UI component for it.
77

88
Modify the files
99
>app/components/Home/Home.component.js
@@ -73,18 +73,19 @@ Modify the files
7373
};
7474
```
7575

76-
Now our app should have a bottom bar with character count and a save button.
77-
Currently we have hard coded the character count to 20.
76+
Now our app should have a bottom bar with a character count and a save button.
77+
78+
Currently, we have hardcoded the character count to 20.
7879

7980
<br/>
8081
<div style="text-align:center">
8182
<img src="/assets/images/9/9.1/9.1-bottom-bar.png" style="width: 40%;display:inline-block;" hspace="40" />
8283
</div>
8384
<br/>
8485

85-
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.
8687
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.
8889

8990
So we modify our components as follows:
9091
>app/components/TextArea/TextArea.component.js
@@ -159,27 +160,28 @@ The character count should now update whenever you enter text on the text field.
159160
<br/>
160161

161162

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.
164166

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.
166168

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.
168170

169171
### Setup
170172

171-
Lets begin by installing few packages.
173+
Let's begin by installing a few packages.
172174

173175
`yarn add redux react-redux redux-promise redux-thunk`
174176

175177
or
176178

177179
`npm install --save redux react-redux`
178180

179-
- **redux** - the main redux library.
180-
- **react-redux** - the react bindings for redux, which makes our life easy when using redux with react.
181+
- **redux** - the main Redux library.
182+
- **react-redux** - the React bindings for Redux, which makes our life easy when using Redux with React.
181183

182-
Additionally, you can also install your preferred redux middleware like `redux-thunk`, etc. The comments on the code specify how to do that.
184+
Additionally, you can also install your preferred Redux middleware like `redux-thunk`, etc. The comments on the code specify how to do that.
183185

184186
Now create the files
185187
>app/redux/store.js
@@ -218,7 +220,7 @@ export default combineReducers({
218220
});
219221
```
220222

221-
Now, lets add our first reducer and action.
223+
Now, let's add our first reducer and action.
222224

223225

224226
Create the files:
@@ -247,7 +249,7 @@ const test = (state = {}, action) => {
247249

248250
export default test;
249251
```
250-
Now lets add our test reducer to the root reducer.
252+
Now let's add our test reducer to the root reducer.
251253

252254
Modify
253255
>app/redux/reducers/root.reducer.js
@@ -261,7 +263,7 @@ export default combineReducers({
261263
});
262264
```
263265

264-
Now lets initialize the store.
266+
Now let's initialize the store.
265267

266268
Modify the file:
267269
>app/index.js
@@ -288,7 +290,7 @@ class NoteTaker extends Component {
288290
export default NoteTaker;
289291
```
290292

291-
and move the Initialization of the home component to another file
293+
and move the initialization of the home component to another file.
292294

293295
>app/App.container.js
294296
@@ -317,23 +319,23 @@ const mapDispatchToProps = (dispatch) => ({
317319
export default connect(mapStateToProps, mapDispatchToProps)(App);
318320
```
319321

320-
At this point, we should a have a redux store with an initial test state from the test reducer.
322+
At this point, we should a have a Redux store with an initial test state from the test reducer.
321323

322-
To check this, lets run our app on the simulator.
324+
To check this, let's run our app on the simulator.
323325

324326
Now open up the debug menu on the iOS simulator by pressing `cmd+ctrl+z` or on Android emulator by using `cmd+m`.
325327

326328
Choose `Debug JS Remotely`.
327329

328-
This should run the app js code in react-native-debugger and if all went well we should see something like this on the console panel:
330+
This should run the app JS code in react-native-debugger and if all goes well we should see something like this on the console panel:
329331

330332
<br>
331333
<div style="text-align:center">
332334
<img src="/assets/images/9/9.1/9.1-redux-setup-complete.png" style="width: 80%;display:inline-block;" hspace="20">
333335
</div>
334336
<br>
335337

336-
This implies that our redux store successfully initialized with the test reducer.
338+
This implies that our Redux store is successfully initialized with the test reducer.
337339

338340
__NOTE:__ If your tests fail due to the error `window not defined`, then add a mock file
339341
>\_\_mocks__/react-native.js

0 commit comments

Comments
 (0)