Actions are nothing but just javascript object.
When we want to change the state of store then there is no option rather than dispatching the actions. Dispatching action is the only way by which we can change the state of store.
Action contains two properties:
1. Type
2. Payload
Type is nothing but simple plain string to identify what kind of action it is e.g "Update_Employee" / "Delete_Employee"
Payload is data required for performing the action e.g suppose we have to dispatch "Update_Employee" Action but, to perform this action we need updated employee's data so that we can alter the state of store
so in this case updated employee's details will be the payload for this action
so basic structure of action will be like below
{ type: "Update_Employee",
payload : {
employeeId : 101,
name : "John Tot",
Address : "-----"
}
}
When we are implementing redux in angular using 'ngrx' we need to import Action from '@ngrx/store'
and for creating any predefined action we need to create class which will implement Action interface
Store has method dispatch() to dispatch the action
Example of Action:
we can dispatch this action in component like below:
this.store is nothing but store which is injected in constructor of component
Component's constructor:
Lets see how action will update the store in next posts. Stay tuned.
← Redux - Store
When we want to change the state of store then there is no option rather than dispatching the actions. Dispatching action is the only way by which we can change the state of store.
Action contains two properties:
1. Type
2. Payload
Type is nothing but simple plain string to identify what kind of action it is e.g "Update_Employee" / "Delete_Employee"
Payload is data required for performing the action e.g suppose we have to dispatch "Update_Employee" Action but, to perform this action we need updated employee's data so that we can alter the state of store
so in this case updated employee's details will be the payload for this action
so basic structure of action will be like below
{ type: "Update_Employee",
payload : {
employeeId : 101,
name : "John Tot",
Address : "-----"
}
}
When we are implementing redux in angular using 'ngrx' we need to import Action from '@ngrx/store'
import { Action } from '@ngrx/store';
and for creating any predefined action we need to create class which will implement Action interface
Store has method dispatch() to dispatch the action
Example of Action:
export class UpdateEmployee implements Action {
readonly type = "Update_Employee";
constructor(public payload:any) { }
}
we can dispatch this action in component like below:
this.store.dispatch(new UpdateEmployee(this.userName));
this.store is nothing but store which is injected in constructor of component
Component's constructor:
constructor(private store: Store<AppState>) { }
Lets see how action will update the store in next posts. Stay tuned.
← Redux - Store
Comments
Post a Comment