Posted in by Tudipat 05:09Comments Off on How to manage state in angular using NgRx?
How to manage state in angular using NgRx?
25 September 2020
What is NgRx?
NgRx is an angular framework to build reactive applications built on top of rxjs. It gives state management to create applications that can be explicitly maintained. So basically it will maintain the data or the state in-store and from that via service or directly we can access it in our component.
Steps to install NgRx:
Step 1: Check for angular cli by ‘ng -v’ in the terminal.
Step 2: Create a workspace for the project.
ng new ngrx-tutorial
Step 3: Get into the workspace and install NgRx package manager.
Installing with npm.
npm install @ngrx/store –save
Installing with yarn.
yarn add @ngrx/store
Installing with ng add.
ng add @ngrx/store
Create a Model:
Model is something that is specific to NgRx in which we can export the interface.
Steps:
Create a folder inside the src folder.
Create a model.ts file inside that folder and add the code from the snapshot in the .ts file.
Create an Action:
Action in NgRx store defines two things:
It’s type in a string basically defining what’s happening there. For eg. remove_tutorial or add_tutorial.
It states the payload of the required data.
Steps:
Create a folder inside the src folder.
Create an actions.ts file inside that folder and add the code from the snapshot in the .ts file.
Part 1: Import action from NgRx store and the model (ngrx Tutorial in our case).
Part 2: Define the action type in a string constant.
Part 3: To pass the data in the payload create a class for each action with the constructor.
Part 4: Export all the action classes to use in the reducer.
Create a Reducer:
The reducer takes the incoming actions and checks what to process with them. It returns a new state and accepts the previous state depends on the action
Steps:
Create a folder for reducer inside src.
Create a reducer.ts file inside that folder and add the code from the snapshot in the .ts file. This is how it works:
Part 1: Created some initial states with some sort of data.
Part 2: It is the actual reducer. It takes the initial state we have defined ‘ngrx Tutorial’ for our case and we will need to pass the action we have imported.
Part 3: We used a switch case to determine the action type that is accessed so when we initially call this reducer and the project loads up, by default it is going to return the state until the action type will be accessed through the dispatch mask method.
Create an App state:
This is the last part we need to do that is to define the app state.
Steps:
Create a file inside the app folder as app.state.ts Import this file in the components in which ngrx will be accessed.
All content provided on this blog is for informational purposes only. Tudip Technologies provides no endorsement and makes no representations as to accuracy, reliability, completeness, suitability or validity of any information or content on, distributed through or linked, downloaded or accessed from this site. Tudip Technologies will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use of the information on this site. All information is provided on an as-is basis without any obligation to make improvements or to correct errors or omissions. This site may contain links to other websites. Tudip Technologies makes no guarantees or promises regarding these websites and does not necessarily endorse or approve of their content. You may not modify any part of the blog. The inclusion of any part of this blog in another work, whether in printed or electronic or other form, or inclusion of any part of the blog in another website by linking, framing or otherwise without the express permission of Tudip Technologies is prohibited. This site may not be used for any illegal or illicit purpose and Tudip Technologies reserves the right, at its sole discretion and without notice of any kind, to remove anything posted to this site. By using this site, you hereby acknowledge that any reliance upon any materials shall be at your sole risk.