Skip to content

Commit b389663

Browse files
committed
migrate store creation and mutable store refs
1 parent 6d1abb5 commit b389663

3 files changed

Lines changed: 143 additions & 59 deletions

File tree

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,73 @@
11
---
22
title: produce
33
use_cases: >-
4-
immutable updates, immer-style mutations, store modifications, complex state
5-
changes, nested updates
4+
store updates, nested store changes, draft-style mutations
65
tags:
76
- store
8-
- immutable
97
- updates
10-
- immer
118
- mutations
129
version: "1.0"
1310
description: >-
14-
Use Immer-inspired produce API to mutate Solid stores immutably. Simplify
15-
complex nested updates with familiar mutation syntax.
11+
Create a store modifier that applies changes by mutating a draft value.
1612
---
1713

18-
`produce` is an [Immer](https://immerjs.github.io/immer/) inspired API for Solid's Store objects that allows the store to be mutated inside the `produce` function.
14+
`produce` creates a store modifier that applies changes by mutating a draft value.
15+
16+
## Import
1917

2018
```ts
2119
import { produce } from "solid-js/store";
22-
import type { NotWrappable, Store } from "solid-js/store";
20+
```
21+
22+
## Type
2323

24-
function produce<T>(
25-
fn: (state: T) => void
26-
): (
27-
state: T extends NotWrappable ? T : Store<T>
28-
) => T extends NotWrappable ? T : Store<T>;
24+
```ts
25+
function produce<T>(fn: (state: T) => void): (state: T) => T;
2926
```
3027

31-
For use with `createStore`:
28+
## Parameters
29+
30+
### `fn`
31+
32+
- **Type:** `(state: T) => void`
33+
34+
Function that mutates the provided draft state.
35+
36+
## Return value
37+
38+
- **Type:** `(state: T) => T`
39+
40+
Store modifier function.
41+
42+
## Behavior
43+
44+
- `produce` returns a function that can be passed to store setters or [`modifyMutable`](/reference/store-utilities/modify-mutable).
45+
- The modifier receives a mutable draft of the current state.
46+
- Nested properties can be changed inside the modifier.
47+
48+
## Examples
49+
50+
### Basic usage
3251

3352
```tsx
34-
import { produce } from "solid-js/store";
53+
import { createStore, produce } from "solid-js/store";
3554

3655
const [state, setState] = createStore({
3756
user: {
3857
name: "John",
39-
age: 30,
4058
},
41-
list: ["book", "pen"],
59+
list: ["book"],
4260
});
4361

4462
setState(
4563
produce((state) => {
4664
state.user.name = "Jane";
47-
state.list.push("pencil");
65+
state.list.push("pen");
4866
})
4967
);
5068
```
69+
70+
## Related
71+
72+
- [`modifyMutable`](/reference/store-utilities/modify-mutable)
73+
- [`reconcile`](/reference/store-utilities/reconcile)
Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,83 @@
11
---
22
title: reconcile
33
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
65
tags:
76
- store
87
- diffing
9-
- api
108
- synchronization
11-
- immutable
129
version: "1.0"
1310
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.
1612
---
1713

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.
2015

21-
```tsx
16+
## Import
17+
18+
```ts
2219
import { reconcile } from "solid-js/store";
23-
import type { NotWrappable, Store } from "solid-js/store";
20+
```
2421

25-
function reconcile<T>(
26-
value: T | Store<T>,
22+
## Type
23+
24+
```ts
25+
function reconcile<T extends U, U>(
26+
value: T,
2727
options?: {
2828
key?: string | null;
2929
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;
3432
```
3533

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`
3947

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.
4149

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
4372

4473
```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: [] });
5077

78+
setState("todos", reconcile([{ id: 1, title: "Write docs" }]));
5179
```
5280

53-
##### Options
81+
## Related
5482

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)
Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,61 @@
11
---
22
title: unwrap
33
use_cases: >-
4-
raw data access, proxy removal, debugging, third-party integration, data
5-
serialization
4+
reading store data without proxies, passing plain data to other APIs
65
tags:
76
- store
87
- proxy
98
- data
10-
- debugging
11-
- utilities
129
version: "1.0"
1310
description: >-
14-
Extract raw data from Solid stores without proxy wrapping. Essential for
15-
debugging, serialization, or third-party integrations.
11+
Remove store proxy wrapping from a store or store subtree.
1612
---
1713

18-
`unwrap` returns the underlying data in the store without a proxy.
14+
`unwrap` removes store proxy wrapping from a store or store subtree.
1915

20-
```tsx
16+
## Import
17+
18+
```ts
2119
import { unwrap } from "solid-js/store";
22-
import type { Store } from "solid-js/store";
20+
```
21+
22+
## Type
2323

24-
function unwrap(store: Store<T>): T;
24+
```ts
25+
function unwrap<T>(item: T): T;
2526
```
27+
28+
## Parameters
29+
30+
### `item`
31+
32+
- **Type:** `T`
33+
34+
Store value or store subtree to unwrap.
35+
36+
## Return value
37+
38+
- **Type:** `T`
39+
40+
Unwrapped value.
41+
42+
## Behavior
43+
44+
- Store proxies are removed recursively from returned objects and arrays.
45+
- Returned objects and arrays are plain values rather than reactive store proxies.
46+
- Non-proxy input values are returned unchanged.
47+
48+
## Examples
49+
50+
### Basic usage
51+
52+
```ts
53+
import { createStore, unwrap } from "solid-js/store";
54+
55+
const [state] = createStore({ user: { name: "John" } });
56+
const user = unwrap(state.user);
57+
```
58+
59+
## Related
60+
61+
- [`createStore`](/reference/store-utilities/create-store)

0 commit comments

Comments
 (0)