React-redux ⇒
React-ReduxRedux is the javascript library which is used for data communication over the app level
It is preffered to use redux when there is many context ( useContext ) you have to use because you have to manually manage the state the the Context.Provider , Context.Consumer and all those thing you have to manage the hiearchy then only it will work
Otherwise we can use the Redux to use all those Context in one place and the states are managed by the Redux
Redux is not specific to React.js . we can use this in any javascript file and start using …
It provides Some functions like
- we have a Store which will have all the states
- Then these States provides the data to components
- Then the componets takes some user input then Some action has been done to Store
- The reducer function is there to update the stated present in the store
- Dispatch is actually a function in redux that will execute the action
How to import and use the redux :
Now when we are executing the dispatch function then we have to provide some type so that only the wanted state will be updated with that action
Suppose we want to create the button for upvote so when we click the button then the value of upvote has to increment and it can be achieve through state manegement
Defining Type in dispatch function
Now we can add some condition in the reducerFunction that when the type : something then do this or if some different then do some other task to the state
State
State is a object having all the state so when you are assigning something to type then you will perform the operation on the State.state_name