Skip to content

Commit 6099591

Browse files
committed
docs: finish vanilla JS quick-start guide
1 parent 1aab40c commit 6099591

File tree

8 files changed

+168
-9
lines changed

8 files changed

+168
-9
lines changed

docs/config.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
{
1717
"label": "Installation",
1818
"to": "installation"
19+
},
20+
{
21+
"label": "Quick Start",
22+
"to": "quick-start"
1923
}
2024
],
2125
"frameworks": [
@@ -88,7 +92,7 @@
8892
{
8993
"label": "Interfaces / StoreOptions",
9094
"to": "reference/interfaces/storeoptions"
91-
}
95+
},
9296
{
9397
"label": "Interfaces / DerivedOptions",
9498
"to": "reference/interfaces/derivedoptions"
@@ -224,4 +228,6 @@
224228
]
225229
}
226230
]
231+
}
232+
]
227233
}

docs/framework/angular/quick-start.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Quick Start
33
id: quick-start
44
---
55

6-
The basic angular app example to get started with the Tanstack angular-store.
6+
The basic angular app example to get started with the TanStack angular-store.
77

88
**app.component.ts**
99
```angular-ts

docs/framework/react/quick-start.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Quick Start
33
id: quick-start
44
---
55

6-
The basic react app example to get started with the Tanstack react-store.
6+
The basic react app example to get started with the TanStack react-store.
77

88
```tsx
99
import React from "react";
@@ -56,4 +56,4 @@ const root = ReactDOM.createRoot(document.getElementById("root"));
5656
root.render(<App />);
5757

5858

59-
```
59+
```

docs/framework/solid/quick-start.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Quick Start
33
id: quick-start
44
---
55

6-
The basic Solid app example to get started with the Tanstack Solid-store.
6+
The basic Solid app example to get started with the TanStack Solid-store.
77

88
```jsx
99
import { useStore, Store } from '@tanstack/solid-store';

docs/framework/svelte/quick-start.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Quick Start
33
id: quick-start
44
---
55

6-
The basic Svelte app example to get started with the Tanstack svelte-store.
6+
The basic Svelte app example to get started with the TanStack svelte-store.
77

88
**store.ts**
99
```ts
@@ -65,4 +65,4 @@ export function updateState(animal: 'cats' | 'dogs') {
6565
</script>
6666

6767
<button onclick={() => updateState(animal)}>My Friend Likes { animal }</button>
68-
```
68+
```

docs/framework/vue/quick-start.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Quick Start
33
id: quick-start
44
---
55

6-
The basic vue app example to get started with the Tanstack vue-store.
6+
The basic vue app example to get started with the TanStack vue-store.
77

88
**App.vue**
99
```html

docs/installation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ TanStack Store is compatible with Vue 2 and 3.
2727
npm install @tanstack/angular-store
2828
```
2929

30-
TanStack Store is compatible with Angular 16+
30+
TanStack Store is compatible with Angular 19+
3131

3232
## SolidJS
3333

docs/quick-start.md

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
---
2+
title: Quick Start
3+
id: quick-start
4+
---
5+
6+
TanStack Store is, first and foremost, a framework-agnostic signals implementation.
7+
8+
It can be used with any of our framework adapters, but can also be used in vanilla JavaScript or TypeScript. It's currently used to power many of our library's internals.
9+
10+
# Store
11+
12+
You'll start by creating a new store instance, which is a wrapper around your data:
13+
14+
```typescript
15+
import { Store } from '@tanstack/store';
16+
17+
const countStore = new Store(0);
18+
19+
console.log(countStore.state); // 0
20+
countStore.setState(() => 1);
21+
console.log(countStore.state); // 1
22+
```
23+
24+
This `Store` can then be used to track updates to your data:
25+
26+
```typescript
27+
const unsub = countStore.subscribe(() => {
28+
console.log('The count is now:', count.state);
29+
});
30+
31+
// Later, to cleanup
32+
unsub();
33+
```
34+
35+
You can even transform the data before it's updated:
36+
37+
```typescript
38+
const count = new Store(12, {
39+
updateFn: (prevValue) => updateValue => {
40+
return updateValue + prevValue;
41+
}
42+
});
43+
44+
count.setState(() => 12);
45+
// count.state === 24
46+
```
47+
48+
And implement a primitive form of derived state:
49+
50+
```typescript
51+
let double = 0;
52+
const count = new Store(0, {
53+
onUpdate: () => {
54+
double = count.state * 2;
55+
}
56+
})
57+
```
58+
59+
## Batch Updates
60+
61+
You can batch updates to a store by using the `batch` function:
62+
63+
```typescript
64+
import { batch } from '@tanstack/store';
65+
66+
// countStore.subscribers will only trigger once at the end with the final state
67+
batch(() => {
68+
countStore.setState(() => 1);
69+
countStore.setState(() => 2);
70+
});
71+
```
72+
73+
# Derived
74+
75+
You can also use the `Derived` class to create derived values that lazily update when their dependencies change:
76+
77+
```typescript
78+
const count = new Store(0);
79+
80+
const double = new Derived({
81+
fn: () => count.state * 2,
82+
// Must explicitly list dependencies
83+
deps: [count]
84+
});
85+
86+
// Must mount the derived value to start listening for updates
87+
const unmount = double.mount();
88+
89+
// Later, to cleanup
90+
unmount();
91+
```
92+
93+
## Previous Deferred Value
94+
95+
You can access the previous value of a derived computation by using the `prevVal` argument passed to the `fn` function:
96+
97+
```typescript
98+
const count = new Store(1);
99+
100+
const double = new Derived({
101+
fn: ({ prevVal }) => {
102+
return count.state + (prevVal ?? 0);
103+
},
104+
deps: [count]
105+
});
106+
107+
double.mount();
108+
double.state; // 1
109+
count.setState(() => 2);
110+
double.state; // 3
111+
```
112+
113+
## Dependency Values
114+
115+
You can access the values of the dependencies of a derived computation by using the `prevDepVals` and `currDepVals` arguments passed to the `fn` function:
116+
117+
```typescript
118+
const count = new Store(1);
119+
120+
const double = new Derived({
121+
fn: ({ prevDepVals, currDepVals }) => {
122+
return (prevDepVals[0] ?? 0) + currDepVals[0];
123+
},
124+
deps: [count]
125+
});
126+
127+
double.mount();
128+
double.state; // 1
129+
count.setState(() => 2);
130+
double.state; // 3
131+
```
132+
133+
# Effects
134+
135+
You can also use the `Effect` class to manage side effects across multiple stores and derived values:
136+
137+
```typescript
138+
const effect = new Effect({
139+
fn: () => {
140+
console.log('The count is now:', count.state);
141+
},
142+
// Array of `Store`s or `Derived`s
143+
deps: [count],
144+
// Should effect run immediately, default is false
145+
eager: true
146+
})
147+
148+
// Must mount the effect to start listening for updates
149+
const unmount = effect.mount();
150+
151+
// Later, to cleanup
152+
unmount();
153+
```

0 commit comments

Comments
 (0)