# uselocalstoragenextjs

> Create hook useLocalStorage for NextJs

Latest version **2.0.2** (published 2024-10-11) · ISC license · 0 weekly downloads

## Install

```sh
npm install uselocalstoragenextjs
pnpm add uselocalstoragenextjs
yarn add uselocalstoragenextjs
bun add uselocalstoragenextjs
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2024-10-11 |
| First published | 2022-08-15 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 13.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | franciscoblancojn |
| Maintainers | franciscoblancojn |
| Keywords | hook, nextjs, localstorage |

## Links

- npm: https://www.npmjs.com/package/uselocalstoragenextjs
- Repository: https://gitlab.com/franciscoblancojn/use-localstorage-nextjs
- Homepage: https://gitlab.com/franciscoblancojn/use-localstorage-nextjs#readme
- Issues: https://gitlab.com/franciscoblancojn/use-localstorage-nextjs/issues
- npm.io page: https://npm.io/package/uselocalstoragenextjs

## Dependencies (2)

- [react](https://npm.io/package/react.md) 18.2.0
- [react-dom](https://npm.io/package/react-dom.md) 18.2.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 2.0.2 (latest) — 2024-10-11
- 2.0.1 — 2024-10-08
- 2.0.0 — 2024-03-08
- 1.2.5 — 2023-08-11
- 1.2.4 — 2023-08-09
- 1.2.3 — 2023-08-09
- 1.2.2 — 2023-01-27
- 1.2.1 — 2023-01-25
- 1.2.0 — 2023-01-25
- 1.1.3 — 2022-11-07
- 1.1.2 — 2022-11-07
- 1.1.1 — 2022-08-20
- 1.1.0 — 2022-08-15
- 1.0.14 — 2022-08-15
- 1.0.13 — 2022-08-15
- … 13 more at https://npm.io/package/uselocalstoragenextjs/versions

## README

# useLocalStorage

This is a library that provides an easy way to manage localStorage, having the possibility to connect changes through different components.

- [Installing](#installing)
- [Import](#import)
- [Use](#use)
  - [Use in multiple components](#use-in-multiple-components)
- [Developer](#developer)
- [Repositories](#repositories)

## Installing

Using npm:

```bash
npm i uselocalstoragenextjs
```

## Import

```javascript
import { useLocalStorage } from "uselocalstoragenextjs";
```

## Use

```javascript
const {
  value, //Value of element in localStorage
  setLocalStorage, //function for modify localStorage
  load, //if the value has been loaded or not
} = useLocalStorage({
  name: "cart", //name of element in localStorage
  defaultValue: [], //defaulValue if element in localStorage if null
  parse: (v: any) => {
    //function for modify value after get of localStorage
    return JSON.parse(v == "" ? "[]" : v);
  },
  updateValue(oldValue, newValue) {
    //function for modify value before set of localStorage
    return [...oldValue, newValue];
  },
});
```

### Use in multiple components

```javascript
import { useLocalStorage } from "uselocalstoragenextjs";
import Component from "./component";

const Home = () => {
  const { value, setLocalStorage, load } = useLocalStorage({
    name: "cart",
    defaultValue: [],
    parse: (v: any) => {
      return JSON.parse(v == "" ? "[]" : v);
    },
    updateValue(olValue, newValue) {
      return [...olValue, newValue];
    },
  });
  return (
    <div>
      Cart
      <div>{JSON.stringify(value)}</div>
      <br />
      <button
        onClick={() => {
          setLocalStorage({ p: 1 });
        }}
      >
        Add New Product
      </button>
      <Component />
    </div>
  );
};

export default Home;
```

```javascript
import { useLocalStorage } from "uselocalstoragenextjs";

export default () => {
  const { value, load } = useLocalStorage({
    name: "cart",
    defaultValue: [],
    parse: (v: any) => {
      return JSON.parse(v == "" ? "[]" : v);
    },
  });
  return (
    <>
      {load && (
        <>
          N Items in Cart
          <br />
          {value.length}
        </>
      )}
    </>
  );
};
```

## Use typescript

```ts
//interface of value
interface notification_interface {
  type?: "ok" | "error" | "warning";
  msg?: string;
}
const {
  value, //Value of element in localStorage
  setLocalStorage, //function for modify localStorage
  load, //if the value has been loaded or not
} = useLocalStorage<notification_interface>({
  name: "notification", //name of element in localStorage
  defaultValue: {}, //defaulValue if element in localStorage if null
  parse: (v: any) => {
    //function for modify value after get of localStorage
    return JSON.parse(v == "" ? "{}" : v);
  },
});
```

## Developer

[Francisco Blanco](https://franciscoblanco.vercel.app/)

[Github franciscoblancojn](https://github.com/franciscoblancojn)

[Email blancofrancisco34@gmail.com](mailto:blancofrancisco34@gmail.com)

## Repositories

- [Github](https://github.com/franciscoblancojn/uselocalstoragenextjs)

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