Socket
Socket
Sign inDemoInstall

@ngxs/store

Package Overview
Dependencies
Maintainers
1
Versions
1301
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ngxs/store


Version published
Weekly downloads
65K
decreased by-42.19%
Maintainers
1
Weekly downloads
 
Created

What is @ngxs/store?

@ngxs/store is a state management library for Angular applications, inspired by the Redux pattern. It provides a simple and intuitive API for managing application state, making it easier to develop, test, and maintain Angular applications.

What are @ngxs/store's main functionalities?

State Management

This code demonstrates how to define a state and an action in @ngxs/store. The `TodoState` class manages a list of todos, and the `AddTodo` action allows adding a new todo to the state.

import { State, Action, StateContext } from '@ngxs/store';

export class AddTodo {
  static readonly type = '[Todo] Add';
  constructor(public payload: string) {}
}

export interface TodoStateModel {
  todos: string[];
}

@State<TodoStateModel>({
  name: 'todos',
  defaults: {
    todos: []
  }
})
export class TodoState {
  @Action(AddTodo)
  add(ctx: StateContext<TodoStateModel>, action: AddTodo) {
    const state = ctx.getState();
    ctx.setState({
      ...state,
      todos: [...state.todos, action.payload]
    });
  }
}

Selectors

Selectors in @ngxs/store are used to query specific pieces of state. This example shows how to create a selector to get the list of todos from the state.

import { Selector } from '@ngxs/store';
import { TodoStateModel } from './todo.state';

export class TodoSelectors {
  @Selector([TodoStateModel])
  static getTodos(state: TodoStateModel) {
    return state.todos;
  }
}

State Context

The StateContext provides methods to interact with the state, such as `getState` and `setState`. This example shows how to use the StateContext to update the state when an action is dispatched.

import { StateContext } from '@ngxs/store';

export class TodoState {
  @Action(AddTodo)
  add(ctx: StateContext<TodoStateModel>, action: AddTodo) {
    const state = ctx.getState();
    ctx.setState({
      ...state,
      todos: [...state.todos, action.payload]
    });
  }
}

Other packages similar to @ngxs/store

Keywords

FAQs

Package last updated on 18 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc