Skip to content

chore: adding more in depth docs for main features #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"label": "Quick Start",
"to": "quick-start"
}

],
"frameworks": [
{
Expand Down Expand Up @@ -70,6 +71,25 @@
}
]
},
{
"label": "Guides & Concepts",
"children": [
{
"label": "Store",
"to": "core-store"
},
{
"label": "Derived",
"to": "core-derived"
},
{
"label": "Effect",
"to": "core-effect"
}
]

},

{
"label": "API Reference",
"children": [
Expand Down
123 changes: 123 additions & 0 deletions docs/core-derived.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
---
title: Core Derived
id: core-derived
---

# Derived Documentation


The `Derived` class creates computed values that automatically update when their dependencies change. This solves the diamond dependency problem through intelligent batching and dependency tracking.

```typescript
import { Derived } from '@tanstack/store'

const doubled = new Derived({
deps: [counterStore],
fn: () => counterStore * 2
})
```

**Key Features:**
- **Automatic Updates**: Recalculates when dependencies change
- **Diamond Dependency Resolution**: Prevents duplicate calculations in complex dependency graphs
- **Lazy Evaluation**: Only computes when subscribed to or explicitly mounted
- **Lazy Evaluation**:Access to both current and previous values of dependencies and the derived value itself




### Derived Options

#### `DerivedOptions<TState, TArr>`
Configuration object for creating derived values.

```typescript
interface DerivedOptions<TState, TArr> {
deps: TArr // Array of Store or Derived dependencies
fn: (props: DerivedFnProps<TArr>) => TState // Computation function
onSubscribe?: (listener: Listener<TState>, derived: Derived<TState>) => () => void
onUpdate?: () => void // Called after each recomputation
}
```

#### `DerivedFnProps<TArr>`
Props passed to the derived computation function.

```typescript
interface DerivedFnProps<TArr> {
prevVal: unknown | undefined // Previous derived value (undefined on first run)
prevDepVals: TUnwrappedArr | undefined // Previous dependency values
currDepVals: TUnwrappedArr // Current dependency values
}
```

Example with previous values:

```typescript
const changeTracker = new Derived({
deps: [counterStore],
fn: ({ currDepVals, prevDepVals, prevVal }) => {
const currentCounter = currDepVals[0]
const prevCounter = prevDepVals?.[0]


return {
current: currentCounter,
previous: prevCounter,
hasChanged: prevCounter !== currentCounter,
previousResult: prevVal
}
}
})
```

### Derived Methods

#### `mount()`
Registers the derived value in the dependency graph and returns cleanup function.

```typescript
derived.mount()
```

#### `recompute()`
Manually triggers recomputation of the derived value.

```typescript
derived.recompute()
```

#### `subscribe(listener)`
Subscribes to derived value changes.

```typescript
const unsubscribe = derived.subscribe(({ prevVal, currentVal }) => {
// Handle derived value change
})
```

#### `checkIfRecalculationNeededDeeply()`
Recursively checks all derived dependencies and recomputes if any dependency values have changed..

```typescript
derived.checkIfRecalculationNeededDeeply()
```

#### `registerOnGraph(deps?)
Registers this derived value in the dependency graph with specified dependencies (defaults to this.options.deps).


```typescript
derived.registerOnGraph()
// or with custom deps
derived.registerOnGraph([customStore])
```


#### `unregisterFromGraph(deps?)`
Removes this derived value from the dependency graph for specified dependencies.

```typescript
derived.unregisterFromGraph()

```
41 changes: 41 additions & 0 deletions docs/core-effect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
title: Core Effect
id: core-effect
---

# Effect Documentation

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, not a maintainer, but just something I think is really important to include in the docs

mount() must be run before the effect will do anything, in addition, mount returns an unmount function which can be used to remove the effect, exactly like you would with the cleanup function in something like useEffect


The `Effect` class runs side effects when dependencies change, similar to `useEffect` in React.

```typescript
import { Effect } from '@tanstack/store'

const logEffect = new Effect({
deps: [counterStore],
fn: () => console.log('Counter changed:', counterStore.state),
eager: true // Run immediately on creation
})
```

### Effect Options

#### `EffectOptions`
Configuration for creating effects.

```typescript
interface EffectOptions {
deps: ReadonlyArray<Derived<any> | Store<any>> // Dependencies to watch
fn: () => void // Effect function to run
eager?: boolean // Whether to run immediately (default: false)
}
```

### Effect Methods

#### `mount()`
Activates the effect

```typescript
effect.mount()

```
Loading