Open History to see the last commit
1---
2title: Quick Start
3id: quick-start
4---
5β
6The basic angular app example to get started with the TanStack angular-store.
7β
8**app.component.ts**
9```angular-ts
10import { Component } from '@angular/core'
11import { DisplayComponent } from './display.component'
12import { IncrementComponent } from './increment.component'
13β
14@Component({
15selector: 'app-root',
16imports: [DisplayComponent, IncrementComponent],
17template: `
18<h1>How many of your friends like cats or dogs?</h1>
19<p>
20 Press one of the buttons to add a counter of how many of your friends like
21 cats or dogs
22</p>
23<app-increment animal="dogs" />
24<app-display animal="dogs" />
25<app-increment animal="cats" />
26<app-display animal="cats" />
27`,
28})
29export class AppComponent {}
30β
31```
32β
33**store.ts**
34```typescript
35import { Store } from '@tanstack/angular-store';
36β
37// You can instantiate the store outside of Angular components too!
38export const store = new Store({
39 dogs: 0,
40 cats: 0,
41});
42β
43export function updateState(animal: 'dogs' | 'cats') {
44 store.setState((state) => {
45 return {
46 ...state,
47 [animal]: state[animal] + 1,
48 };
49 });
50}
51```
52β
53**display.component.ts**
54```angular-ts
55import { injectStore } from '@tanstack/angular-store';
56import { store } from './store';
57β
58@Component({
59 selector: 'app-display',
60 template: `
61 <!-- This will only re-render when animal changes. If an unrelated store property changes, it won't re-render -->
62 <div>{{ animal() }}: {{ count() }}</div>
63 `,
64 standalone: true
65})
66export class Display {
67 animal = input.required<string>();
68 count = injectStore(store, (state) => state[this.animal()]);
69}
70```
71β
72**increment.component.ts**
73```angular-ts
74import { injectStore } from '@tanstack/angular-store';
75import { store, updateState } from './store';
76β
77@Component({
78 selector: 'app-increment',
79 template: `
80 <button (click)="updateState(animal())">My Friend Likes {{ animal() }}</button>
81 `,
82 standalone: true
83})
84export class Increment {
85 animal = input.required<string>();
86 updateState = updateState;
87}
88```