|
1 | 1 | --- |
2 | 2 | title: reconcile |
3 | 3 | use_cases: >- |
4 | | - api responses, data synchronization, immutable data diffing, observable |
5 | | - subscriptions, large data updates |
| 4 | + syncing store state, applying immutable data updates, diffing incoming data |
6 | 5 | tags: |
7 | 6 | - store |
8 | 7 | - diffing |
9 | | - - api |
10 | 8 | - synchronization |
11 | | - - immutable |
12 | 9 | version: "1.0" |
13 | 10 | description: >- |
14 | | - Efficiently diff and merge data changes with reconcile. Perfect for syncing |
15 | | - API responses or handling immutable data updates. |
| 11 | + Create a store modifier that reconciles existing state with a new value. |
16 | 12 | --- |
17 | 13 |
|
18 | | -`reconcile` is designed for diffing data changes in situations where granular updates cannot be applied. |
19 | | -This is useful when dealing with immutable data from stores or handling large API responses. |
| 14 | +`reconcile` creates a store modifier that reconciles existing state with a new value. |
20 | 15 |
|
21 | | -```tsx |
| 16 | +## Import |
| 17 | + |
| 18 | +```ts |
22 | 19 | import { reconcile } from "solid-js/store"; |
23 | | -import type { NotWrappable, Store } from "solid-js/store"; |
| 20 | +``` |
24 | 21 |
|
25 | | -function reconcile<T>( |
26 | | - value: T | Store<T>, |
| 22 | +## Type |
| 23 | + |
| 24 | +```ts |
| 25 | +function reconcile<T extends U, U>( |
| 26 | + value: T, |
27 | 27 | options?: { |
28 | 28 | key?: string | null; |
29 | 29 | merge?: boolean; |
30 | | - } = { key: "id" } |
31 | | -): ( |
32 | | - state: T extends NotWrappable ? T : Store<T> |
33 | | -) => T extends NotWrappable ? T : Store<T>; |
| 30 | + } |
| 31 | +): (state: U) => T; |
34 | 32 | ``` |
35 | 33 |
|
36 | | -`reconcile` has a `key` option that can be used when available to match items. |
37 | | -The `value` accepts either a value of type `T` or a Store containing values of type `T`. |
38 | | -This represents the data to be reconciled. |
| 34 | +## Parameters |
| 35 | + |
| 36 | +### `value` |
| 37 | + |
| 38 | +- **Type:** `T` |
| 39 | + |
| 40 | +New value to reconcile against the current state. |
| 41 | + |
| 42 | +### `options` |
| 43 | + |
| 44 | +#### `key` |
| 45 | + |
| 46 | +- **Type:** `string | null` |
39 | 47 |
|
40 | | -The `reconcile` function helps manage data changes by performing a diffing process, making it particularly handy in scenarios where applying granular updates is challenging or inefficient. |
| 48 | +Key used to match items during reconciliation. |
41 | 49 |
|
42 | | -The `key` and `merge` options provide flexibility to customize the reconciliation process based on specific needs. |
| 50 | +#### `merge` |
| 51 | + |
| 52 | +- **Type:** `boolean` |
| 53 | + |
| 54 | +Controls whether reconciliation pushes updates to the leaves instead of replacing non-matching branches. |
| 55 | + |
| 56 | +## Return value |
| 57 | + |
| 58 | +- **Type:** `(state: U) => T` |
| 59 | + |
| 60 | +Store modifier function. |
| 61 | + |
| 62 | +## Behavior |
| 63 | + |
| 64 | +- `reconcile` performs a diff between the current state and `value`. |
| 65 | +- With the default `key`, array items are matched by `"id"`. |
| 66 | +- When `merge` is `false`, non-matching branches can be replaced. |
| 67 | +- When `merge` is `true`, reconciliation pushes updates deeper into the structure. |
| 68 | + |
| 69 | +## Examples |
| 70 | + |
| 71 | +### Basic usage |
43 | 72 |
|
44 | 73 | ```ts |
45 | | -// subscribing to an observable |
46 | | -const unsubscribe = store.subscribe(({ todos }) => ( |
47 | | - setState('todos', reconcile(todos)); |
48 | | -); |
49 | | -onCleanup(() => unsubscribe()); |
| 74 | +import { createStore, reconcile } from "solid-js/store"; |
| 75 | + |
| 76 | +const [state, setState] = createStore({ todos: [] }); |
50 | 77 |
|
| 78 | +setState("todos", reconcile([{ id: 1, title: "Write docs" }])); |
51 | 79 | ``` |
52 | 80 |
|
53 | | -##### Options |
| 81 | +## Related |
54 | 82 |
|
55 | | -| Option | Type | Default | Description | |
56 | | -| ------ | ------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
57 | | -| key | string | "id" | Specifies the key to be used for matching items during reconciliation | |
58 | | -| merge | boolean | false | When merge is false, referential checks are performed where possible to determine equality, and items that are not referentially equal are replaced. When merge is true, all diffing is pushed to the leaves, effectively morphing the previous data to the new value. | |
| 83 | +- [`produce`](/reference/store-utilities/produce) |
0 commit comments