In Redux framework there is a store which maintain state of application (store is single source of truth). so the store is use throughout the application.
all components share the store. So its very easy to have communication in between the components regardless of at what level component comes.
As we have seen in previous post whenever we wants to change the state then we need to dispatch action. but updating store is not taken care by dispatching action only. we need to handled that dispatched action and do the update.
As we have to maintain state as mutable we need a function which will be the pure function which will change the state and return new state.
Yes, reducer is that function. We already defined reducer.
Reducer function takes 2 parameters
1. State - current state of application
2. action - action to perform
According to action it update the state and return new state
Lets take an example I have a store which containes employee list
Lets assume we have an action 'Add_Employee' which adds employee to state
So we will dispatch an action like this (below code appear from where you want to dispatch an action and for that you need to inject store in constructor)
Now we need to handle this action in reducer
so reducer will be like below
This is very straightforward code.
We have added case for 'Add_Employee' action in reducer in that we have added employee in existing list and return cloned state (since state is mutable)
In Next post we will see how to handle database updation (Side Effects) in Redux. Stay tuned.
← Redux - Action
all components share the store. So its very easy to have communication in between the components regardless of at what level component comes.
As we have seen in previous post whenever we wants to change the state then we need to dispatch action. but updating store is not taken care by dispatching action only. we need to handled that dispatched action and do the update.
As we have to maintain state as mutable we need a function which will be the pure function which will change the state and return new state.
Yes, reducer is that function. We already defined reducer.
"Reducer is pure function which alter the state of store and return the new state"
So how can we write reducers in AngularReducer function takes 2 parameters
1. State - current state of application
2. action - action to perform
According to action it update the state and return new state
Lets take an example I have a store which containes employee list
export interface State {
employees: any[];
}
export const initialState: State = {
employees: []
};
Lets assume we have an action 'Add_Employee' which adds employee to state
So we will dispatch an action like this (below code appear from where you want to dispatch an action and for that you need to inject store in constructor)
this.store.dispatch(new AddEmployeeAction({Id:101,Name:"test",Gender:"F"}))
Now we need to handle this action in reducer
so reducer will be like below
export function reducer(state: State = initialState, action: any): State {
switch (action.type) {
case "Add_Employee":
if (action.payload) {
let employees = state.employees;
employees.push(payload);//add new employee
return Object.assign({}, state, {
employees: employees
});
}
}
}
This is very straightforward code.
We have added case for 'Add_Employee' action in reducer in that we have added employee in existing list and return cloned state (since state is mutable)
In Next post we will see how to handle database updation (Side Effects) in Redux. Stay tuned.
← Redux - Action
Comments
Post a Comment