As a React application grows, passing state through props across many levels of the component tree — a pattern known as prop drilling — becomes cumbersome and brittle. Global state management libraries solve this by maintaining a single shared store that any component can read from or write to directly. Zustand is one of the lightest solutions available: it requires no wrapping provider, no action types, and no reducers. You define a store withDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/Byrontosh/FundamentosReact/llms.txt
Use this file to discover all available pages before exploring further.
create, export it, and call it like a hook wherever you need it.
The store
storeGalleta.jsx defines the global store for the galleta (cookie) data. Zustand’s create function receives a callback that has access to set, the primitive used to merge new values into the store, and must return an object representing the initial state and all actions.
create— imported directly fromzustand, it is the only API you need to build a store. It takes a setup function and returns a custom hook (storeGalleta) that any component can call.detalle— the initial state object, holdingnombre: "Nucita"andtipo: "galleta". Any component that subscribes tostoreGalletawill receive this value until it is updated.setGalleta— the single action in this store. It accepts anewGalletaobject and callsset({ detalle: newGalleta }), which performs a shallow merge, replacingdetallewith the new value and triggering a re-render in every subscribed component.
Reading state in a parent component
Septimo.jsx is the parent component that renders the Octavo child and also reads from the same storeGalleta store directly.
const { detalle, setGalleta } = storeGalleta() destructures the current detalle value and the setGalleta action from the store. When the Cambiar button is clicked, setGalleta is called with a new object { nombre: "BIMBO", tipo: "Ponkey" }. Because both Septimo and Octavo subscribe to the same store, both components re-render simultaneously with the updated data — no prop passing required.
Reading state in a child component
Octavo.jsx is the child component rendered inside Septimo. It subscribes to storeGalleta independently, without receiving any props from its parent.
Octavo only destructures detalle — it has no need for setGalleta because it is a read-only display component. When Septimo calls setGalleta, Zustand notifies all subscribers, so Octavo receives the new detalle value and re-renders automatically, even though nothing was passed down through props.
Store shape
The following table documents every field exposed bystoreGalleta.
| Field | Type | Description |
|---|---|---|
detalle | { nombre: string, tipo: string } | The current galleta object. Starts as { nombre: "Nucita", tipo: "galleta" } and is replaced wholesale by setGalleta. |
setGalleta | (newGalleta: object) => void | Action that calls Zustand’s set to replace detalle with the provided object and notify all subscribers. |
Zustand stores are singletons —
create returns a single shared hook instance that is reused across every import. You can call storeGalleta() in any component anywhere in the tree without wrapping your application in a <Provider>. This is one of Zustand’s primary advantages over the React Context API, which requires a provider at or above every consumer in the component hierarchy.