All Projects → nas5w → use-local-storage

nas5w / use-local-storage

Licence: MIT license
A flexible React Hook for using Local Storage.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to use-local-storage

east-store
east-store is a state manager with easiest api that based hooks and immer.
Stars: ✭ 18 (-68.42%)
Mutual labels:  react-hooks
sharyn
🌹 Sharyn – A collection of JavaScript / TypeScript packages that make your life easier and reduce your boilerplate code
Stars: ✭ 30 (-47.37%)
Mutual labels:  react-hooks
use-query-string
🆙 A React hook that serializes state into the URL query string
Stars: ✭ 50 (-12.28%)
Mutual labels:  react-hooks
react-redux-dashboard
React Redux Dashboard with redux-saga and local-storage support
Stars: ✭ 48 (-15.79%)
Mutual labels:  local-storage
google-place-autocomplete
🏆 Best practice with Google Place Autocomplete API on React
Stars: ✭ 68 (+19.3%)
Mutual labels:  react-hooks
MovieCards
React App that uses TMDb API to display movie data. Try it out! ->
Stars: ✭ 38 (-33.33%)
Mutual labels:  react-hooks
flhooks
React like Hooks implementation for Flutter.
Stars: ✭ 38 (-33.33%)
Mutual labels:  react-hooks
jedisdb
redis like key-value state management solution for React
Stars: ✭ 13 (-77.19%)
Mutual labels:  react-hooks
react-antd-admin
基于vite2.x + react17.x + typescript4.x + antd4.x + react-router6.x + mobx6.x编写的一款简单高效的前后端分离的权限管理系统
Stars: ✭ 140 (+145.61%)
Mutual labels:  react-hooks
concave
🧐 Lens-like state management (for React).
Stars: ✭ 13 (-77.19%)
Mutual labels:  react-hooks
react-use-storage
React Hook to handle local and session storage
Stars: ✭ 18 (-68.42%)
Mutual labels:  react-hooks
resynced
An experimental hook that lets you have multiple components using multiple synced states using no context provider
Stars: ✭ 19 (-66.67%)
Mutual labels:  react-hooks
use-app-state
🌏 useAppState() hook. that global version of setState() built on Context.
Stars: ✭ 65 (+14.04%)
Mutual labels:  react-hooks
style-hook
use css in js with react hook.
Stars: ✭ 16 (-71.93%)
Mutual labels:  react-hooks
pojo-observer
A minimalist object observer with React hooks support. Allows you to separate concerns between presentation and interaction logic
Stars: ✭ 91 (+59.65%)
Mutual labels:  react-hooks
react-cool-form
😎 📋 React hooks for forms state and validation, less code more performant.
Stars: ✭ 246 (+331.58%)
Mutual labels:  react-hooks
react-2048
A React implementation of 2048 game built with typescript and styled-components
Stars: ✭ 66 (+15.79%)
Mutual labels:  react-hooks
acharep
🏠 App to find fraternities near your university
Stars: ✭ 23 (-59.65%)
Mutual labels:  react-hooks
use-fetch-hook
A custom hook to fetch and cache data
Stars: ✭ 95 (+66.67%)
Mutual labels:  react-hooks
aimscroll
🍹 Painless utility libary to handle scroll positions and methods in react
Stars: ✭ 12 (-78.95%)
Mutual labels:  react-hooks

useLocalStorage React hook

A flexible React Hook for using Local Storage.

Build Status Codecov Status


useLocalStorage React hook demo


Installation

Install with npm

npm i use-local-storage

Install with yarn

yarn add use-local-storage

Basic Usage

In its most basic form, the useLocalStorage hook just needs the Local Storage key you wish to use. However, it's advised that you also provde a default value as a second argument in the event that they key does not yet exist in Local Storage.

The following usage will persist the username variable in a "name" key in Local Storage. It will have a default/initial value of an empty string "". This default value will only be used if there is no value already in Local Storage, moreover setting the variable username to undefined will remove it from Local Storage.

import useLocalStorage from "use-local-storage";

function MyComponent() {
  const [username, setUsername] = useLocalStorage("name", "");

  return (
    <>
      <input
        value={username}
        onChange={(e) => {
          setUsername(e.target.value);
        }}
      />
      <button
        onClick={() => {
          setUsername(undefined);
        }}
      >
        Remove Username
      </button>
    </>
  );
}

Note: By default, the useLocalStorage hook uses JSON.stringify and JSON.parse to serialize and parse the data, respectively. A custom serializer and/or parser can be configured if desired (discussed below in the Advanced Usage section).

Typescript Use

The type of username will be inferred from your default value. In this case, the type of string will be inferred.

If you use useLocalStorage without providing a default value, or you simply want to specify that username is actually of a different type, you should pass the type of your data as a generic:

import useLocalStorage from "use-local-storage";

function MyComponent() {
  const [username, setUsername] = useLocalStorage<string>("name");

  return (
    <>
      <input
        value={username}
        onChange={(e) => {
          setUsername(e.target.value);
        }}
      />
      <button
        onClick={() => {
          setUsername(undefined);
        }}
      >
        Remove Username
      </button>
    </>
  );
}

Advanced Usage / Options

the useLocalStorage hook takes an optional third options argument. This allows you to configure a custom serializer and/or parser if you need to use something other than JSON.stringify and JSON.parse. The options object also has a logger key to log an errors caught in the hook. You can also disable the cross-context synchronization by setting syncData to false.

const options = {
  serializer: (obj) => {
    /* Serialize logic */
    return someString;
  },
  parser: (str) => {
    /* Parse logic */
    return parsedObject;
  },
  logger: (error) => {
    // Do some logging
  },
  syncData: false // You can disable cross context sync
};

const [data, setData] = useLocalStorage("data", { foo: "bar" }, options);

Attribution

Storage icon made by DinosoftLabs from www.flaticon.com
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].