<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: seif hassan</title>
    <description>The latest articles on DEV Community by seif hassan (@seifhassan89).</description>
    <link>https://dev.to/seifhassan89</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1033028%2F3cafa148-5e43-4801-ac3a-034c7c5154af.jpeg</url>
      <title>DEV Community: seif hassan</title>
      <link>https://dev.to/seifhassan89</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/seifhassan89"/>
    <language>en</language>
    <item>
      <title>Part Two: NGXS State Management: Beginner's Guide.</title>
      <dc:creator>seif hassan</dc:creator>
      <pubDate>Tue, 04 Jul 2023 07:42:53 +0000</pubDate>
      <link>https://dev.to/seifhassan89/part-two-ngxs-state-management-beginners-guide-4fb7</link>
      <guid>https://dev.to/seifhassan89/part-two-ngxs-state-management-beginners-guide-4fb7</guid>
      <description>&lt;p&gt;&lt;a href="https://dev.tourl"&gt;For part one..&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How are the different concepts organised?
&lt;/h2&gt;

&lt;p&gt;Now that you have gained familiarity with the various concepts of NGXS, let's explore how they interact with each other through a schema, which provides a clear visual representation of their relationships and interactions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pKauG_af--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ozq0ap7gwp5a8adnxytl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pKauG_af--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ozq0ap7gwp5a8adnxytl.png" alt="Image description" width="800" height="538"&gt;&lt;/a&gt;&lt;br&gt;
&lt;u&gt;Schema explaining NGXS functioning&lt;/u&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  After we have set up NGXS, Now lets dive deep into our example:
&lt;/h2&gt;

&lt;p&gt;add the following code in the imports of the &lt;code&gt;@NgModule:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;NgxsModule.forRoot([TodoState], {&lt;br&gt;
  developmentMode: !environment.production&lt;br&gt;
}),&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  like this:
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    ...
    NgxsModule.forRoot([TodoState], {
      developmentMode: !environment.production
    }),
  ]
})
export class AppModule { }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;p&gt;As we discussed earlier in the theoretical part, everything begins with a component. Therefore, let's proceed by writing the code for the &lt;code&gt;app.component.ts&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Component } from '@angular/core';
import {Select, Store} from '@ngxs/store';
import {AddTodo, EmptyTodo} from '../store/todo.actions';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  newTodo = '';

  constructor(private readonly store: Store) {}

  onAddTodo(): void {
    if (this.newTodo.length &amp;gt; 0) {
      this.store.dispatch(new AddTodo(this.newTodo));
    }
    this.newTodo = '';
  }

  onEmptyList(): void {
    this.store.dispatch(new EmptyTodo());
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Let’s create the Actions in the &lt;code&gt;todo.actions.ts&lt;/code&gt;:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export class AddTodo {
  static readonly type = '[Todo] Add';

  constructor(public newTodo: string) {
  }
}

export class EmptyTodo {
  static readonly type = '[Todo] Empty';
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Since the &lt;code&gt;addTodo&lt;/code&gt; action requires the new todo as an input, we need to declare the constructor with &lt;code&gt;newTodo&lt;/code&gt; as a parameter.&lt;/p&gt;

&lt;p&gt;Following the triggering of these actions, certain code will be executed to modify the state. This is where the &lt;code&gt;todo.state.ts&lt;/code&gt; file comes into play.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {Action, State, StateContext} from '@ngxs/store';
import {AddTodo, EmptyTodo} from './todo.actions';

export interface TodoStateModel {
  todoList: string[];
}

@State&amp;lt;TodoStateModel&amp;gt;({
  name: 'todo',
  defaults: {
    todoList: [],
  }
})
export class TodoState {

  @Action(AddTodo)
  addTodo({patchState, getState}: StateContext&amp;lt;TodoStateModel&amp;gt;, {newTodo}: AddTodo): void {
    patchState({todoList: [...getState().todoList, newTodo]});
  }

  @Action(EmptyTodo)
  emptyTodo({patchState}: StateContext&amp;lt;TodoStateModel&amp;gt;): void {
    patchState({todoList: []});
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;@State&lt;/code&gt; decorator allows us to define the initial state of the state container.&lt;/p&gt;

&lt;p&gt;The functions preceded by the &lt;code&gt;@Action&lt;/code&gt; annotation are associated with an action and will be executed when triggered.&lt;/p&gt;




&lt;h2&gt;
  
  
  Let's take a closer look at the more complex &lt;code&gt;addTodo()&lt;/code&gt; function to understand its components:
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;First, it takes two parameters:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{patchState, getState}: StateContext&amp;lt;TodoStateModel}&lt;/code&gt;, which provides &lt;code&gt;patchState()&lt;/code&gt; and &lt;code&gt;getState()&lt;/code&gt; functions to interact with the state defined in the same file.&lt;br&gt;
&lt;code&gt;{newTodo}: AddTodo&lt;/code&gt;, which allows us to access data from the triggered action.&lt;/p&gt;




&lt;p&gt;As mentioned earlier, the state is immutable. To modify the value of one or more state elements, NGXS provides the &lt;code&gt;patchState()&lt;/code&gt; function. Here's an example of how it's used:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;patchState({todoList: [...getState().todoList, newTodo]});&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This function enables developers to specify parameters only for the fields they intend to modify, without needing to create a copy of the entire state.&lt;/p&gt;

&lt;h2&gt;
  
  
  To allow our component to access the &lt;code&gt;todoList&lt;/code&gt; value from the state, we can add a getter function. Therefore, the &lt;code&gt;todo.state.ts&lt;/code&gt; file is updated as follows:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {Action, Selector, State, StateContext} from '@ngxs/store';
import {AddTodo, EmptyTodo} from './todo.actions';

export interface TodoStateModel {
  todoList: string[];
}

@State&amp;lt;TodoStateModel&amp;gt;({
  name: 'todo',
  defaults: {
    todoList: [],
  }
})
export class TodoState {

  @Selector()
  static getTodoList(state: TodoStateModel): string[] {
    return state.todoList;
  }

  @Action(AddTodo)
  addTodo({patchState, getState}: StateContext&amp;lt;TodoStateModel&amp;gt;, {newTodo}: AddTodo): void {
    patchState({todoList: [...getState().todoList, newTodo]});
  }

  @Action(EmptyTodo)
  emptyTodo({patchState}: StateContext&amp;lt;TodoStateModel&amp;gt;): void {
    patchState({todoList: []});
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can observe, we have added the &lt;code&gt;getTodoList()&lt;/code&gt; function, which takes the state as a parameter and is annotated with &lt;code&gt;@Selector()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Whenever the value of &lt;code&gt;todoList&lt;/code&gt; changes, the code within the selector function is executed, and any component that listens to todoList through &lt;code&gt;getTodoList()&lt;/code&gt; will automatically receive the updated value.&lt;/p&gt;




&lt;p&gt;As mentioned earlier, this function acts as a decorator for the returned value. Therefore, if you decide to sort todoList in alphabetical order within the selector, each component listening to todoList through this function will receive the sorted list.&lt;/p&gt;

&lt;p&gt;To complete the loop, we need to return to our App component to allow it to access the value of todoList from the state using the newly created selector. To achieve this, we add a new variable in our component preceded by the &lt;a class="mentioned-user" href="https://dev.to/select"&gt;@select&lt;/a&gt;() annotation, with the selector function as the parameter.&lt;/p&gt;

&lt;h2&gt;
  
  
  Here is the final version of &lt;code&gt;app.component.ts&lt;/code&gt;:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Component } from '@angular/core';
import {Select, Store} from '@ngxs/store';
import {AddTodo, EmptyTodo} from '../store/todo.actions';
import {TodoState} from '../store/todo.state';
import {Observable} from 'rxjs';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  @Select(TodoState.getTodoList) todoList$?: Observable&amp;lt;string[]&amp;gt;;

  newTodo = '';

  constructor(private readonly store: Store) {}

  onAddTodo(): void {
    if (this.newTodo.length &amp;gt; 0) {
      this.store.dispatch(new AddTodo(this.newTodo));
    }
    this.newTodo = '';
  }

  onEmptyList(): void {
    this.store.dispatch(new EmptyTodo());
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;🥳🙌👏Congratulations! You have successfully learned how to set up and utilize &lt;strong&gt;NGXS&lt;/strong&gt;. Your next task, should you decide to accept it, is to adapt the example we have worked on to meet the requirements of your own project.&lt;/p&gt;

&lt;p&gt;For further information on &lt;strong&gt;NGXS&lt;/strong&gt;, you can refer to the official documentation, available &lt;a href="https://www.ngxs.io/concepts/store"&gt;HERE&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Well done, and best of luck with your NGXS implementation! 💫👋👋&lt;/p&gt;

</description>
    </item>
    <item>
      <title>NGXS State Management: Beginner's Guide.</title>
      <dc:creator>seif hassan</dc:creator>
      <pubDate>Tue, 27 Jun 2023 10:21:08 +0000</pubDate>
      <link>https://dev.to/seifhassan89/ngxs-state-management-beginners-guide-2fe7</link>
      <guid>https://dev.to/seifhassan89/ngxs-state-management-beginners-guide-2fe7</guid>
      <description>&lt;h4&gt;
  
  
  Discover the simplicity of handling the state of your #Angular application through #NGXS and grasp the knowledge of integrating NGXS.
&lt;/h4&gt;

&lt;h2&gt;
  
  
  State Management
&lt;/h2&gt;

&lt;p&gt;State Management involves utilizing a Design Pattern to effectively utilize a single data source across various components of an application. This approach offers advantages like data reliability, simplified code maintenance, and more.&lt;/p&gt;

&lt;h2&gt;
  
  
  NGXS
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.ngxs.io/"&gt;NGXS&lt;/a&gt; is an Angular-centric State Management Pattern that serves as a centralized data source for your application's state, offering straightforward implementation. In my view, NGXS is easier to adopt compared to NGRX&lt;/p&gt;

&lt;p&gt;In NGXS, there are four key concepts to incorporate:&lt;/p&gt;

&lt;h2&gt;
  
  
  Store
&lt;/h2&gt;

&lt;p&gt;The store in NGXS acts as a central state manager, allowing for action dispatching and enabling the selection of specific data slices from the global state.&lt;/p&gt;

&lt;h2&gt;
  
  
  Actions
&lt;/h2&gt;

&lt;p&gt;Actions in NGXS represent specific tasks or operations that can be performed to achieve desired outcomes. For instance, we can dispatch a "Get Books" action to retrieve books. When defining actions, it is necessary to specify a type that describes the nature of the action.&lt;/p&gt;




&lt;h2&gt;
  
  
  State
&lt;/h2&gt;

&lt;p&gt;States in NGXS are responsible for defining the container that holds the application's state. It is possible to have various fundamental states that are commonly utilized in applications, such as:&lt;/p&gt;

&lt;p&gt;1.✌️ An initial state &lt;br&gt;
2.🔃 A loading state&lt;br&gt;
3.👎 An error state&lt;br&gt;
4.☑️ A success state&lt;/p&gt;


&lt;h2&gt;
  
  
  Select
&lt;/h2&gt;

&lt;p&gt;Selects in NGXS are functions that enable us to retrieve specific data from our store. There are two approaches to perform this:&lt;/p&gt;

&lt;p&gt;Using decorators:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export class NameYourComponent {

  @Select(TodosState) todos$: Observable&amp;lt;Todos[]&amp;gt;;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using store selection functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export class NameYourComponent {
todos$: Observable&amp;lt;Todos[]&amp;gt;;

  constructor(private _store: Store) {
    this.todos$ = this._store.select(state =&amp;gt; state.todos.todos);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;With a fundamental understanding of State Management, NGXS, and its underlying concepts, we are now ready to apply it in a practical application!&lt;/p&gt;




&lt;h2&gt;
  
  
  To begin, install the necessary dependencies by following these steps:
&lt;/h2&gt;

&lt;p&gt;(NGXS): Install on the project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i @ngxs/store
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Helper plugins
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;devtools plugin
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i @ngxs/devtools-plugin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To Use it Install this Extension&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://chrome.google.com/webstore/detail/redux-devtools/lmhkpmbekcpmknklioeibfkpmmfibljd?hl=pt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;logger plugin
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install @ngxs/logger-plugin --save
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or if you are using yarn:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add @ngxs/logger-plugin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we just have to add the module to our imports in the @NgModule of our application like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {NgxsLoggerPluginModule} from '@ngxs/logger-plugin';

@NgModule({
  imports: [
    ...
    NgxsLoggerPluginModule.forRoot({
       disabled: environment.production
     }),
  ],
  ...
})
export class AppModule { }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;a href="//www.face%20book.com"&gt;&lt;strong&gt;Part Two..&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>redux</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
