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
In this file, you will write the functions that dispatch an action. These functions will be linked to your component props by the container’s mapDispatchToProps function. At that point, a component should be able to call a prop like a function and know that it will lead to an action getting dispatched. After the action is dispatched, it will be heard by only one type of function: the reducer.
4
+
1
5
### An action is a plain object describing what happened. For example:
Object serialization is the process of converting an object’s state to a string from which it can later be restored. ECMAScript 5 provides native functions JSON.stringify() and JSON.parse() to serialize and restore JavaScript objects. These functions use the JSON data interchange format.
37
-
By convention, the top-level state in React, is an object or some other key-value collection like a Map, but technically it can be any type. Still, you should do your best to keep the state serializable. Don't put anything inside it that you can't easily turn into JSON.
38
44
45
+
By convention, the top-level state in React, is an object or some other key-value collection like a Map, but technically it can be any type. Still, you should do your best to keep the state serializable. Don't put anything inside it that you can't easily turn into JSON.
39
46
40
47
## Another working example of action dispatching to reducer and updating state
41
48
@@ -49,17 +56,15 @@ In ..actions/itemActions.js I have the below
49
56
50
57
```js
51
58
exportconstgetItems= () =>dispatch=> {
52
-
53
-
dispatch(setItemsLoading()); // because I want the loading to be set to true for now.
54
-
55
-
axios.get('/api/items')
56
-
.then(res=> {
57
-
dispatch({
58
-
type:GET_ITEMS,
59
-
payload:res.data
60
-
})
61
-
})
62
-
}
59
+
dispatch(setItemsLoading()); // because I want the loading to be set to true for now.
60
+
61
+
axios.get("/api/items").then(res=> {
62
+
dispatch({
63
+
type:GET_ITEMS,
64
+
payload:res.data
65
+
});
66
+
});
67
+
};
63
68
```
64
69
65
70
And in ..reducers/itemReducer.js I have -
@@ -74,8 +79,8 @@ switch(action.type) {
74
79
}
75
80
```
76
81
77
-
So, ``getItems()`` function is the action, and when its invoked or run, then it will dispatch this ``action.type``, which is ``GET_ITEMS`` to the reducers. And then in the reducer I will just return the state ( with spread operator ``...state`` ), and bring it into my component.
82
+
So, `getItems()` function is the action, and when its invoked or run, then it will dispatch this `action.type`, which is `GET_ITEMS` to the reducers. And then in the reducer I will just return the state ( with spread operator `...state` ), and bring it into my component.
78
83
79
-
And the way, I invoke this function in my reducer is by doing the ``action.type`` and then applying various cases. And because of the mechanism of ``dispatch`` function, when I apply ``action.type`` and case ``GET_ITEM`` I dispatch ``getItems()`` function from my action to reducer.
84
+
And the way, I invoke this function in my reducer is by doing the `action.type` and then applying various cases. And because of the mechanism of `dispatch` function, when I apply `action.type` and case `GET_ITEM` I dispatch `getItems()` function from my action to reducer.
80
85
81
-
By the mechanism of ``dispatch()`` - I am using dispatch() to send the type along with the data that we get from the axios request to the backend. And note, that the main function getItem() dispatches another function ( setItemsLoading ). This second function is called a thunk, and it returns the object/action. In the context of redux-thunk, a thunk is a second function that performs delayed logic by being asynchronously returned by a first function.
86
+
By the mechanism of `dispatch()` - I am using dispatch() to send the type along with the data that we get from the axios request to the backend. And note, that the main function getItem() dispatches another function ( setItemsLoading ). This second function is called a thunk, and it returns the object/action. In the context of redux-thunk, a thunk is a second function that performs delayed logic by being asynchronously returned by a first function.
Copy file name to clipboardExpand all lines: Redux/README.md
+20-2
Original file line number
Diff line number
Diff line change
@@ -30,11 +30,13 @@ This means that all data in an application follows the same lifecycle pattern, m
30
30
31
31
## An example of How data and state flows in Redux
32
32
33
-
Basically in React the `setState()` method is the initial and final step for changing the state and re-rendering the component. But with Redux this state changing will have to go through 5 / 6 different steps before state change can happen. Here's an example
33
+
Basically in React the `setState()` method is the initial and final step for changing the state and re-rendering the component. But with Redux this state changing will have to go through 5 / 6 different steps before state change can happen. Here's an example. All these because in Redux all the application's state lives in one immutable state-tree called store. That store is at the fundamental level is a simple javascript object. And the reason its called **immutable** is because one does not simply modify the state tree. What we do is, we distribute action. **State is read-only**: The state cannot be changed directly by the view or any other process (maybe as a result of network callback or some other event). In order to change the state, you must express your intent by emitting an action. An action is a plain object describing your intent, and it contains a type property and some other data. Actions can be logged and later replayed which makes it good for debugging and testing purpose.
34
34
35
35
### 1> User types in an input box.
36
36
37
-
### 2> That calls an action-creator to get a well-formed action.
37
+
### 2> That calls an action-creator to get a well-formed action. Action-Creator is just a function and creates an object.
38
+
39
+
Object goes to the reducer, which in turn is just a function that takes an object. And what comes out of it is also just an object.
38
40
39
41
### 3> The action then is dispatched to Redux - Redux then inserts that action to a Root-reducer.
40
42
@@ -43,3 +45,19 @@ Basically in React the `setState()` method is the initial and final step for cha
43
45
### 5> That reducer returns a new state and given the old state and the action object, the new state becomes the stored state.
44
46
45
47
### 6> That store then is fed-back into React and calls forth the final updated state.
48
+
49
+
Again I’ll walk through a simple example to show how a user input could trigger a state change that is reflected by a different part of the screen.
50
+
51
+
The user clicks a button in the app and a component prop is called like a function.
52
+
53
+
The corresponding container dispatches an action. This happens because the prop (which was just called in the container) is tied to an action dispatcher using mapDispatchToProps (in the container).
54
+
55
+
A reducer ‘hears’ that action and runs a function which returns a new state with specific modifications.
56
+
57
+
The container ‘knows’ that state has changed and modifies a specific prop in the component as a result of the mapStateToProps function.
58
+
59
+
The component now has a prop that has officially changed due to a new state being generated, so if that prop is responsible for any any visible UI, the user will see it change automatically.
Copy file name to clipboardExpand all lines: Redux/Reducers.md
+10-6
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,15 @@
1
-
### Reducers - Absolute Bas
1
+
### Reducers - Absolute Basic
2
2
3
3
### Reducer is just a function that takes two arguments A) Action (which is just a plain JS object preferably with type property ) and B) It takes the current state of your application. And then new state of the world comes out as the output of reducers.
4
4
5
5
### And how the action object is created - by something called Action creator, which is a function and the purpose of which is to create objects. So Action Creators creates objects -> Objects goes to Reducers (which is just a function) -> And ultimately what comes out, is the new state which is just an object.
6
6
7
+
### The Action Creator.
8
+
9
+
In this file, you will write the functions that dispatch an action. These functions will be linked to your component props by the container’s mapDispatchToProps function. At that point, a component should be able to call a prop like a function and know that it will lead to an action getting dispatched. After the action is dispatched, it will be heard by only one type of function: the reducer.
10
+
11
+
The Reducer is the last step of the process of user-input generating a state-change and rendering the changed state back to the view. After ‘hearing’ an action get dispatched, the reducer can now generate a new state based on what the action wants it to do. Note the the state never actually changes in Redux, but instead the reducer generates a new state which is a copy of the old state, but with some sort of modification.
12
+
7
13
<imgsrc="reducers.svg">
8
14
9
15
1> https://redux.js.org/basics/reducers
@@ -24,10 +30,9 @@ Call non-pure functions, e.g. Date.now() or Math.random().
24
30
25
31
For now, just remember that the reducer must be pure. Given the same arguments, it should calculate the next state and return it. No surprises. No side effects. No API calls. No mutations. Just a calculation.
26
32
33
+
2> https://daveceddia.com/what-is-a-reducer/
27
34
28
-
2> https://daveceddia.com/what-is-a-reducer/
29
-
30
-
Redux is basically a fancy ``Array.reduce`` function. A Redux reducer function has this signature:
35
+
Redux is basically a fancy `Array.reduce` function. A Redux reducer function has this signature:
31
36
32
37
## (state, action) => newState
33
38
@@ -37,7 +42,7 @@ As in: it takes the current state, and an action, and returns the newState. Look
37
42
38
43
Plainly speaking, a Redux reducer gets to decide how each action affects the state.
39
44
40
-
## 3> Reducers Must be Pure Functions
45
+
## 3> Reducers Must be Pure Functions
41
46
42
47
In order to achieve deterministic state reproduction, reducers must be pure functions. No exceptions. A pure function:
43
48
@@ -52,4 +57,3 @@ Array parameters are also references. You can’t just `.push()` new items to an
Copy file name to clipboardExpand all lines: Redux/container-component.md
+7-1
Original file line number
Diff line number
Diff line change
@@ -6,4 +6,10 @@ The container component that knows about redux and dispatch and store and state.
6
6
7
7
The wrapped-component (i.e. which is not a container) in the pattern, which does the rendering doesn't need to know about any of that stuff: it gets its method to call via its props.
8
8
9
-
And ... mapDispatchToProps was the useful means that redux provides to let the container-component easily pass that function into the wrapped component on its props.
9
+
And ... mapDispatchToProps was the useful means that redux provides to let the container-component easily pass that function into the wrapped component on its props.
10
+
11
+
The Container is a file that corresponds directly to a single component. **Think of it like a literal container that you put your component in to ‘upgrade’ it.** By using two container functions called `mapDispatchToProps` and `mapStateToProps`, you can now link up your component’s props to state and action dispatchers (which are used to change state). If you had a div in your component that you want to display a property of state, you can simply make that div display a prop and then use mapStateToProps to link up that prop to state. If you want your component to store something in state, you can create a prop, call it as a function, and then use mapDispatchToProps to dispatch an action.
1> We create a store, using the **createStore** function. We pass it our reducer, and then use the compose function to create a single function from two other functions. I won't get too much into how it works, but it takes other middleware and returns a single enhancer function from it. We are creating our middleware using the applyMiddleware function and passing it our thunk library.
1
+
1> We create a store, using the **createStore** function. We pass it our reducer, and then use the compose function to create a single function from two other functions. It takes other middlewares and returns a single enhancer function from it. We are creating our middleware using the applyMiddleware function and passing it our thunk library.
Creates a Redux that holds the complete state tree of your app.
21
25
22
26
There should only be a single store in your app.
23
27
24
28
#### Arguments
25
29
26
-
1.`reducer`*(Function)*: A **reducing** function that returns the next state tree, given the current state tree and an **action** to handle.
30
+
1.`reducer`_(Function)_: A **reducing** function that returns the next state tree, given the current state tree and an **action** to handle.
27
31
28
-
2.[`preloadedState`]*(any)*: The initial state. You may optionally specify it to hydrate the state from the server in universal apps, or to restore a previously serialized user session. If you produced `reducer` with `combineReducers`, this must be a plain object with the same shape as the keys passed to it. Otherwise, you are free to pass anything that your `reducer` can understand.
32
+
2.[`preloadedState`]_(any)_: The initial state. You may optionally specify it to hydrate the state from the server in universal apps, or to restore a previously serialized user session. If you produced `reducer` with `combineReducers`, this must be a plain object with the same shape as the keys passed to it. Otherwise, you are free to pass anything that your `reducer` can understand.
29
33
30
-
3.[`enhancer`]*(Function)*: The store enhancer. You may optionally specify it to enhance the store with third-party capabilities such as middleware, time travel, persistence, etc. The only store enhancer that ships with Redux is `applyMiddleware()`
34
+
3.[`enhancer`]_(Function)_: The store enhancer. You may optionally specify it to enhance the store with third-party capabilities such as middleware, time travel, persistence, etc. The only store enhancer that ships with Redux is `applyMiddleware()`
31
35
32
36
#### Returns
33
-
*`Store`* : An object that holds the complete state of your app. The only way to change its state is by dispatching actions. You may also [subscribe](Store.md#subscribe) to the changes to its state to update the UI.
37
+
38
+
_`Store`_ : An object that holds the complete state of your app. The only way to change its state is by dispatching actions. You may also [subscribe](Store.md#subscribe) to the changes to its state to update the UI.
0 commit comments