useStore
Subscribe to store changes and trigger component re-renders automatically
API Reference
Syntax
const state = useStore(store, options?)Parameters
store: ZenBox store instanceoptions(optional):pick: Array of keys for selective subscriptiondeep: Enable deep equality checking for nested objects (default: false)
Returns
Current store state or selected fields when using the pick option.
Examples
Full Store Subscription
Subscribe to all store changes for complete reactivity:
import { createStore, useStore } from "zenbox";
const userStore = createStore({
  name: "John",
  age: 30,
  email: "john@example.com",
});
function UserProfile() {
  const user = useStore(userStore);
  return (
    <div>
      <h1>{user.name}</h1>
      <p>Age: {user.age}</p>
      <p>Email: {user.email}</p>
    </div>
  );
}Selective Subscription
Optimize performance by subscribing only to the fields you need:
function UserName() {
  // Re-renders only when 'name' changes
  const { name } = useStore(
    userStore,
    (state) => state.name // use `selector` to subscribe to specific fields
  );
  return <h1>{name}</h1>;
}
function UserContact() {
  // Re-renders only when 'name' or 'email' changes
  const { name, email } = useStore(userStore, {
    pick: ["name", "email"], // use `pick` to subscribe to specific fields
  });
  return (
    <div>
      <p>Name: {name}</p>
      <p>Email: {email}</p>
    </div>
  );
}Deep Comparison
For complex nested objects that require deep comparison:
const settingsStore = createStore({
  theme: {
    colors: { primary: "#blue", secondary: "#gray" },
    typography: { fontSize: 16, fontFamily: "Arial" },
  },
  preferences: {
    notifications: true,
    autoSave: false,
  },
});
function ThemeDisplay() {
  // Deep diffing for nested object changes
  const { theme } = useStore(settingsStore, {
    pick: ["theme"],
    deep: true,
  });
  return (
    <div style={{ color: theme.colors.primary }}>
      Theme: {theme.colors.primary}
    </div>
  );
}Best Practices
✅ Use selective subscription for large stores
// Good: Only subscribe to needed fields
const { name, status } = useStore(largeStore, {
  pick: ["name", "status"],
});❌ Avoid full subscription when you only need specific fields
// Inefficient: Subscribes to all changes
const store = useStore(largeStore);
return <div>{store.name}</div>; // Only uses name✅ Prefer shallow watching for performance
// Default shallow comparison is usually sufficient
const user = useStore(userStore);❌ Don't use deep watching unless necessary
// Expensive: Only use when you need deep object comparison
const data = useStore(store, { deep: true });Hook Comparison
| Hook | Purpose | Re-renders | Best For | 
|---|---|---|---|
useStore | Full store subscription | ✅ Yes | All store data | 
usePick | Selective field subscription | ✅ Yes | Specific fields | 
useComputed | Derived reactive values | ✅ Yes | Calculated data | 
useWatch | Side effects on changes | ❌ No | Logging, APIs | 
Related Hooks
usePick- Convenient shorthand for field selectionuseComputed- Create derived reactive valuesuseWatch- Watch changes with side effects