A code along project to learn React and Redux from React Nanodegree.
The store consists of the following.
- the state tree.
- a way to get the state tree.
- a way to listen and respond to the state changing.
- a way to update the state.
-
Created the store.
-
Created the state variable. local(private)
-
Created the get state function to access the state variable.(it returns an object)
-
Created a Listeners object
-
Update the state of the store --Rule to follow before updating the state
- Only an event can change the state of the store.
(The object created to keep track of the event taking place in redux application is called Action. When an event takes place an object is created. the object looks like the code below.
{ type: "Add_TODO" todo: { id: 0, name: 'learn redux', complete: false } }
the object is called an Action. Every action must have a type property. ) - The function that returns the new state needs to be a pure function. the reason pure function is great is because its predictable ( The function need to pass this three requirements before it can be considered a pure function. They always return the same result if the same arg are passed in They depend solely on the arguement passed in Does not produce any side effect )
The todos function is called a reducer because it takes in the state tree and action and reduces it into a new state.
Create an updater function to update the state (
const dispatch = (action) => { state = reducer(state, action) listeners.forEach((listener) => listener()) }
) - Only an event can change the state of the store.
(The object created to keep track of the event taking place in redux application is called Action. When an event takes place an object is created. the object looks like the code below.
Create a container function for the reducers(root reducer).
Then pass in the name of the root reducer into the store as the main reducer.