Loading Blog Article...
Loading Blog Article...
Loading Blog Article...
SolidJS's createStore offers a powerful way to manage global state without relying on React-style Context. This approach can lead to cleaner code and better performance. Let's dive deep into how it works.
createStore is a primitive in SolidJS that creates a reactive store for managing state. Unlike React's Context, it doesn't require wrapping components in providers or consumers.
import { createStore } from "solid-js/store";
const [state, setState] = createStore({
count: 0,
user: {
name: "John",
age: 25
}
});
Traditional Context-based state management has several drawbacks:
The diagram below illustrates the basic architecture of createStore in SolidJS. In this architecture, createStore creates a Global Store that can be directly accessed and used by multiple components (Component A, B, C). This design eliminates the nested hierarchy of traditional Context API, making state management more direct and efficient. Each component can independently subscribe to and update data in the store without going through intermediate layers like Providers or Consumers.
Let's create a practical example of a global store for a shopping cart:
// store/cart.js
import { createStore } from "solid-js/store";
export const [cart, setCart] = createStore({
items: [],
total: 0
});
export function addItem(item) {
setCart("items", (items) => [...items, item]);
setCart("total", (t) => t + item.price);
}
export function removeItem(itemId) {
setCart("items", (items) => {
const item = items.find(i => i.id === itemId);
setCart("total", (t) => t - item.price);
return items.filter(i => i.id !== itemId);
});
}
Let's break down the implementation of this shopping cart store in detail:
The benefits of this implementation include:
Here's how to use the store in different components without Context:
// components/ProductList.jsx
import { cart, addItem } from "../store/cart";
function ProductList() {
return (
<div>
<For each={products()}>
{(product) => (
<div>
<h3>{product.name}</h3>
<button onClick={() => addItem(product)}>
Add to Cart (${product.price})
</button>
</div>
)}
</For>
</div>
);
}
// components/Cart.jsx
import { cart, removeItem } from "../store/cart";
function Cart() {
return (
<div>
<h2>Shopping Cart (Total: ${cart.total})</h2>
<For each={cart.items}>
{(item) => (
<div>
{item.name} - ${item.price}
<button onClick={() => removeItem(item.id)}>Remove</button>
</div>
)}
</For>
</div>
);
}
Let's analyze this code implementation in detail:
In the ProductList component:
In the Cart component:
The advantages of this implementation are:
For more complex applications, you can combine multiple stores:
// store/auth.js
export const [auth, setAuth] = createStore({
user: null,
isAuthenticated: false
});
// store/theme.js
export const [theme, setTheme] = createStore({
mode: "light",
colors: {
primary: "#007bff",
secondary: "#6c757d"
}
});
// Using multiple stores
function App() {
return (
<div class={theme.mode}>
{auth.isAuthenticated
? <Dashboard user={auth.user} />
: <Login />
}
</div>
);
}
Let's examine the advanced patterns demonstrated in the code above:
This pattern is particularly useful for large applications where different types of state need to be managed separately while still being easily accessible throughout the application.
SolidJS's createStore provides a simple yet powerful way to manage global state without the complexity of Context. Its fine-grained reactivity and straightforward API make it an excellent choice for applications of any size.
By avoiding Context, we get better performance, simpler code, and easier testing, while maintaining all the benefits of a well-structured state management solution.
Back