Skip to content

Commit 71efa87

Browse files
Merge pull request #255 from chris-mcdonald-dev/store-subscriptions
2 parents a9cfa53 + cf6c4e6 commit 71efa87

1 file changed

Lines changed: 28 additions & 4 deletions

File tree

content/references/api-reference/stores/using-stores.mdx

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,39 @@ const [state, setState] = createStore({
9191
Note that modifying an array inside a store will not trigger computations that subscribe to the array directly. For example:
9292

9393
```ts
94+
// After Solid 1.4.0
9495
createEffect(() => {
95-
console.log(state.todos);
96+
// This will not get triggered because we've subscribed to the array itself.
97+
console.log(todos);
9698
});
99+
setTodos(todos.length, { id: 3 });
97100

98-
//This will not trigger the effect:
101+
102+
// Prior to Solid 1.4.0
103+
createEffect(() => {
104+
// This will not get triggered because we've subscribed to the array itself.
105+
console.log(state.todos);
106+
});
99107
setState(todos, state.todos.length, { id: 3 });
108+
```
109+
However, subscribing to any signals that change inside the array will trigger those computations.
110+
```ts
111+
// After Solid 1.4.0
112+
createEffect(() => {
113+
// This will be triggered because we've subscribed to the actual signals in the array.
114+
console.log([...todos]);
115+
console.log(todos[2]) // Or we can subscribe only to a specific one.
116+
});
117+
setTodos(todos.length, { id: 3 });
118+
100119

101-
//This will trigger the effect, because the array reference changes:
102-
setState("todos", (prev) => [...prev, { id: 3 }]);
120+
// Prior to Solid 1.4.0
121+
createEffect(() => {
122+
// This will be triggered because we've subscribed to the actual signals in the array.
123+
console.log([...state.todos]);
124+
console.log(state.todos[2]) // Or we can subscribe only to a specific one.
125+
});
126+
setState(todos, state.todos.length, { id: 3 });
103127
```
104128

105129
## Updating Stores

0 commit comments

Comments
 (0)