# @ribeirolabs/local-storage

> Local storage helper for set/get values, with value conversion

Latest version **3.1.1** (published 2024-06-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install @ribeirolabs/local-storage
pnpm add @ribeirolabs/local-storage
yarn add @ribeirolabs/local-storage
bun add @ribeirolabs/local-storage
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.1.1 |
| Published | 2024-06-30 |
| First published | 2022-02-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 16.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Igor Ribeiro |
| Keywords | local, storage, helper, expiration |

## Links

- npm: https://www.npmjs.com/package/@ribeirolabs/local-storage
- Repository: https://github.com/ribeirolabs/local-storage
- Homepage: https://github.com/ribeirolabs/local-storage#readme
- Issues: https://github.com/ribeirolabs/local-storage/issues
- npm.io page: https://npm.io/package/@ribeirolabs/local-storage

## Alternatives

- [localforage](https://npm.io/package/localforage.md) — 6.2M weekly downloads
- [localforage-observable](https://npm.io/package/localforage-observable.md) — 30.8K weekly downloads
- [@y/y](https://npm.io/package/@y/y.md) — 30.1K weekly downloads
- [@metaobjectsdev/render](https://npm.io/package/@metaobjectsdev/render.md) — 3.5K weekly downloads
- [@ledgerhq/coin-algorand](https://npm.io/package/@ledgerhq/coin-algorand.md) — 1.1K weekly downloads

## Recent versions

- 3.1.1 (latest) — 2024-06-30
- 3.1.0 — 2024-06-30
- 3.0.1 — 2022-08-26
- 3.0.0 — 2022-08-25
- 2.1.0 — 2022-08-03
- 2.0.0 — 2022-02-15
- 1.0.3 — 2022-02-15
- 1.0.2 — 2022-02-15
- 1.0.1 — 2022-02-15
- 1.0.0 — 2022-02-15

## README

# @ribeirolabs/local-storage

Local storage helper for set/get values, with value conversion.

## Usage

### setLocalStorage `(name, value) => void`

Sets `name` with `value` in localStorage, it converts everything but strings with `JSON.stringify` so objects and arrays are stored correctly.

```js
import { setLocalStorage } from '@ribeirolabs/local-storage';

setLocalStorage('person', { name: 'Jhon' });
setLocalStorage('active', true);
setLocalStorage('ids', [1, 2]);
```

### getLocalStorage `(name, fallback) -> value`

Returns the value from localStorage or fallback if it's not present.

LocalStorage stores the values as a string, this function converts the value to the correct type.

```js
import { getLocalStorage } from '@ribeirolabs/local-storage';

getLocalStorage('person', { name: 'default' }) // { name: 'Jhon' }
getLocalStorage('active', false) // true
getLocalStorage('ids', []) // [1, 2]
getLocalStorage('missing', 'use-me') // "use-me"
```

### removeLocalStorage `(name) -> void`

No magic here, it removes `name` from localStorage.

## React

### useLocalStorage `(name, fallback) -> [value, setValue]`

Behaves like `useState`, but `setValue` updates the `value` in localStorage.

```jsx
import { useLocalStorage } from '@ribeirolabs/local-storage/react';

function Component() {
  // `value` will be synced in localStorage
  const [value, setValue] = useLocalStorage('name', 'Jhon Doe');

  return (
    <input name="name" value={value} onChange={e => setValue(e.target.value)} />
  );
}
```

### useSetLocalStorage `(name, value) -> void`

Use this when you need to sync the `value` into localStorage on every change.

```jsx
import { useState } from 'react';
import { useLocalStorage } from '@ribeirolabs/local-storage/react';

function Component() {
  const [value, setValue] = useState('Jhon Doe');

  // updates `name` on localStorage everytime the `value` changes
  useSetLocalStorage('name', value);

  return (
    <input name="name" value={value} onChange={e => setValue(e.target.value)} />
  );
}
```

## Browser

### index.browser.js

It adds a global `ribeirolabs.localStorage` with all the methods.

```html
<script src="https://unpkg.com/@ribeirolabs/local-storage/index.browser.js"></script>

<script>
  ribeirolabs.localStorage.setLocalStorage('name', 'John Doe');

  console.log(
    ribeirolabs.localStorage.getLocalStorage('name', '')
  );

  ribeirolabs.localStorage.removeLocalStorage('name');
</script>
```

### index.mjs

To work with modules

```html
<script type="module">
  import { setLocalStorage, getLocalStorage } from "https://unpkg.com/@ribeirolabs/local-storage/index.mjs";

  setLocalStorage('name', 'John Doe');

  console.log(
    getLocalStorage('name', '')
  );
</script>
```

---
_Source: https://npm.io/package/@ribeirolabs/local-storage · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
