Documentation 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.
localStorage is a synchronous browser API that lets you store key-value pairs that survive page reloads and even full browser restarts. Unlike React state, which resets every time a component unmounts, data written to localStorage stays available until it is explicitly removed or the user clears their browser storage. The challenge is bridging the two worlds: React’s reactive state system and the imperative localStorage API. The typical pattern combines useEffect to write data at the right lifecycle moment and a plain function (or another useEffect) to read it back into state on demand.
Core localStorage methods
ThelocalStorage object exposes a small, focused API. The three methods used most often in React applications are listed below.
| Method | Signature | Description |
|---|---|---|
setItem | setItem(key: string, value: string) => void | Writes a string value under the given key. If the key already exists its value is overwritten. |
getItem | getItem(key: string) => string | null | Returns the string stored under key, or null if the key does not exist. |
removeItem | removeItem(key: string) => void | Deletes the entry for key. Subsequent calls to getItem for that key will return null. |
JSON serialization
localStorage only stores strings. If you try to store a plain JavaScript object directly, it is coerced to the string "[object Object]", which is not useful. The solution is to serialize with JSON.stringify before writing and deserialize with JSON.parse after reading.
JSON.stringify converts any serializable JavaScript value — objects, arrays, numbers, booleans — into a JSON string. JSON.parse reverses the process. Always pair them: data written with JSON.stringify must be read with JSON.parse, otherwise you get a raw string instead of a usable object.
React integration demo
TheDecimo component shows a complete, working example: on mount it writes a user object to localStorage, a button reads and displays the data, and a second button removes it and resets state.
-
useEffect(() => { ... }, [])— the empty dependency array means this effect runs exactly once, immediately after the component mounts. It serializes theuserobject withJSON.stringifyand writes it tolocalStorageunder the key"token-user". This simulates what would happen in a real app after a successful login. -
obtenerToken— called when the user clicks Obtener Token. It reads the raw JSON string fromlocalStoragewithgetItem("token-user"), deserializes it withJSON.parse, and stores the resulting object in theuserTokenstate. Because state updates trigger a re-render, the component immediately displays thename,rol, andtokenfields. -
limpiarToken— called when the user clicks Limpiar Token. It callslocalStorage.removeItem("token-user")to delete the entry from the browser storage, then setsuserTokentonull. The optional chaining (userToken?.name,userToken?.rol,userToken?.token) in the JSX prevents a crash whenuserTokenisnullafter clearing.
localStorage is synchronous — every setItem, getItem, and removeItem call blocks the main JavaScript thread until it completes. For small strings and compact JSON objects this is imperceptible. However, storing large payloads (long arrays, binary data encoded as Base64, deeply nested objects) can introduce noticeable jank. Keep individual entries small, and consider IndexedDB for anything that exceeds a few kilobytes.