Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ngrx/component-store

Package Overview
Dependencies
Maintainers
4
Versions
77
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ngrx/component-store

Reactive store for component state

  • 18.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
212K
increased by6.03%
Maintainers
4
Weekly downloads
 
Created

What is @ngrx/component-store?

@ngrx/component-store is a library for managing local/component state in Angular applications. It provides a simple and reactive way to manage state within Angular components, leveraging RxJS for state management and side effects.

What are @ngrx/component-store's main functionalities?

State Management

This feature allows you to manage the state within a component. The `ComponentStore` class is used to define the state and provide methods to update it.

```typescript
import { ComponentStore } from '@ngrx/component-store';

interface State {
  count: number;
}

@Injectable()
class CounterStore extends ComponentStore<State> {
  constructor() {
    super({ count: 0 });
  }

  readonly increment = this.updater((state) => ({ count: state.count + 1 }));
  readonly decrement = this.updater((state) => ({ count: state.count - 1 }));
  readonly reset = this.updater(() => ({ count: 0 }));
}
```

Selectors

Selectors are used to derive and expose slices of the state. The `select` method allows you to create observables that emit state changes.

```typescript
@Injectable()
class CounterStore extends ComponentStore<State> {
  constructor() {
    super({ count: 0 });
  }

  readonly count$ = this.select(state => state.count);
}
```

Effects

Effects are used to handle side effects, such as HTTP requests. The `effect` method allows you to define observables that can trigger state changes based on external events.

```typescript
@Injectable()
class CounterStore extends ComponentStore<State> {
  constructor(private readonly http: HttpClient) {
    super({ count: 0 });
  }

  readonly loadCount = this.effect((trigger$) =>
    trigger$.pipe(
      switchMap(() => this.http.get<number>('/api/count').pipe(
        tapResponse(
          (count) => this.patchState({ count }),
          (error) => console.error(error)
        )
      ))
    )
  );
}
```

Other packages similar to @ngrx/component-store

Keywords

FAQs

Package last updated on 29 Oct 2024

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

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc