|
2 | 2 | title: "Stores" |
3 | 3 | order: 5 |
4 | 4 | --- |
| 5 | + |
| 6 | +Stores are a state management primitive in Solid that provide a convenient, centralized way to handle structured data. |
| 7 | +Unlike signals, which are best suited for primitive values, stores are better for managing complex data structures like objects and arrays: |
| 8 | + |
| 9 | +- They provide fine-grained reactivity, meaning only the parts of the store that are accessed will trigger updates when they change. |
| 10 | +- They support **nested reactivity,** so you can track and update deeply nested properties without losing reactivity. |
| 11 | +- They make working with complex state easier while avoiding duplication or unnecessary recomputation. |
| 12 | + |
| 13 | +Stores are built on top of Solid's reactive system, leveraging [JavaScript's proxies](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) to track property access and changes. |
| 14 | + |
| 15 | +## Creating a Store |
| 16 | + |
| 17 | +To create a store, you can use the `createStore` function from the `solid-js/store` package. |
| 18 | +This function takes an initial value and returns a tuple containing the store and a setter function to update it. |
| 19 | + |
| 20 | +```tsx |
| 21 | +import { createStore } from "solid-js/store" |
| 22 | + |
| 23 | +// Initialize store |
| 24 | +const [store, setStore] = createStore({ |
| 25 | + userCount: 3, |
| 26 | + users: [ |
| 27 | + { |
| 28 | + id: 0, |
| 29 | + username: "felix909", |
| 30 | + location: "England", |
| 31 | + loggedIn: false, |
| 32 | + }, |
| 33 | + { |
| 34 | + id: 1, |
| 35 | + username: "tracy634", |
| 36 | + location: "Canada", |
| 37 | + loggedIn: true, |
| 38 | + }, |
| 39 | + { |
| 40 | + id: 2, |
| 41 | + username: "johny123", |
| 42 | + location: "India", |
| 43 | + loggedIn: true, |
| 44 | + }, |
| 45 | + ], |
| 46 | +}) |
| 47 | +``` |
| 48 | + |
| 49 | +## Accessing store values |
| 50 | + |
| 51 | +Unlike signals, which are functions that need to be called to get their values, stores can be accessed directly like regular objects or arrays. |
| 52 | + |
| 53 | +```tsx |
| 54 | +console.log(store.userCount) // 3 |
| 55 | +``` |
| 56 | + |
| 57 | +Inside a tracking scope, store properties can be accessed directly, and any changes to those properties will trigger updates to the scope. |
| 58 | + |
| 59 | +:::note |
| 60 | + |
| 61 | +Stores create signals **lazily**. |
| 62 | +This means that a property only becomes reactive once accessed inside a tracking scope. |
| 63 | +::: |
| 64 | + |
| 65 | +```tsx |
| 66 | +const App = () => { |
| 67 | + const [mySignal, setMySignal] = createSignal("This is a signal.") |
| 68 | + const [store, setStore] = createStore({ |
| 69 | + userCount: 3, |
| 70 | + users: [ |
| 71 | + { |
| 72 | + id: 0, |
| 73 | + username: "felix909", |
| 74 | + location: "England", |
| 75 | + loggedIn: false, |
| 76 | + }, |
| 77 | + { |
| 78 | + id: 1, |
| 79 | + username: "tracy634", |
| 80 | + location: "Canada", |
| 81 | + loggedIn: true, |
| 82 | + }, |
| 83 | + { |
| 84 | + id: 2, |
| 85 | + username: "johny123", |
| 86 | + location: "India", |
| 87 | + loggedIn: true, |
| 88 | + }, |
| 89 | + ], |
| 90 | + }) |
| 91 | + return ( |
| 92 | + <div> |
| 93 | + <h1>Hello, {store.users[0].username}</h1> {/* Accessing a store value */} |
| 94 | + <span>{mySignal()}</span> {/* Accessing a signal */} |
| 95 | + </div> |
| 96 | + ) |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +## |
0 commit comments