-
-
Notifications
You must be signed in to change notification settings - Fork 52
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
enyelsequeira
wants to merge
1
commit into
TanStack:main
Choose a base branch
from
enyelsequeira:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--- | ||
title: Core Effect | ||
id: core-effect | ||
--- | ||
|
||
# Effect Documentation | ||
|
||
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() | ||
|
||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 likeuseEffect