-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathReact-Redux-notes.txt
77 lines (57 loc) · 2.62 KB
/
React-Redux-notes.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
What is Redux?
***************
Redux is a predictable state container for JavaScript apps.
> Redux is a library for JavaScript Applications.
> You can use Redux together with React, or with any other view library(Angular, Vue).
> Redux is a state container.
Example- registration form
Core Concepts of Redux
***********************
Store ::➡ Holds state of your application.
Action ::➡ Describe the changes in the state of application.
Reducer ::➡ Actually carries out the state transition dependending on the action.
Example ➡ Book Shop
Shop ➡➡➡➡➡➡ ShopKeeper ➡➡➡➡ Customer
(store) (Reducer) (action)
Rules of Redux
*****************
> The state of your application is stored in an object tree within a single store.
{
NumberOfBooks : 10
}
> Only wayto change the state is to emit an action an object describing what happened.
{Type : "buyBook"}
> To specify how the state tree is transformed by actions, we write pure Reducer.
How to install Redux in react app
**********************************
to install redux in react use "npx install redux react-redux" command
Action in Redux
****************
> Actions are JavaScript object that contains information.
> Actions are the only source of information for the store. It only tells us what has happened.
> Actions have a type property and it should be defined in string consistraint.
> It is compulsory to include the type property in the object.
Syntax:
const Actions = {
Type : 'TypeOfAction'
}
Reducers in React
******************
> Reducers decides how the state of application changes depending upon the action sent to the store.
> Reducers are the function that accepts state and action as parameter and returns the next state of the application.
(previousState, action) => newState.
Redux Store
*************
> Entire Applicaton contains Single Store.
> It is responsible for holding application state.
> getState() method gives access to state it holds.
> dispatch(action) method allow state to be updated
> it has subscribe(listener) method as well by which we can register listeners.
This methd accepts funtion (listener) as a parameter which execute anytime when the state in Redux store changes.
React Redux + Hooks
********************
React Redux offers set of hooks to - subscribe to redux store and dispatch actions.
useSelector Hook-
> useSelector is a hook react-redux library provides to get hold of any state that is maintained in the redux store.
Syntax - const xyz = useSelector(selector: function, equalityFn: function)
Selector function accepts the redux state as its argument and return a value.