All Projects → polemius → Recoil Persist

polemius / Recoil Persist

Licence: mit
Package for recoil state manager to persist and rehydrate store

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Recoil Persist

persistence
💾 Persistence provides a pretty easy API to handle Storage's implementations.
Stars: ✭ 18 (-72.73%)
Mutual labels:  storage, localstorage, sessionstorage
Angular Locker
🗄️ A simple & configurable abstraction for local/session storage in angular js projects
Stars: ✭ 318 (+381.82%)
Mutual labels:  storage, localstorage, sessionstorage
stoor
Storage wrapper with support for namespacing, timeouts and multi get/set and remove.
Stars: ✭ 26 (-60.61%)
Mutual labels:  storage, localstorage, sessionstorage
vue-web-storage
Vue.js plugin for local storage and session storage (1.8 kb min+gz) 💾
Stars: ✭ 85 (+28.79%)
Mutual labels:  storage, localstorage, sessionstorage
Brownies
🍫 Tastier cookies, local, session, and db storage in a tiny package. Includes subscribe() events for changes.
Stars: ✭ 2,386 (+3515.15%)
Mutual labels:  storage, localstorage, sessionstorage
Store
A better way to use localStorage and sessionStorage
Stars: ✭ 1,646 (+2393.94%)
Mutual labels:  storage, localstorage, sessionstorage
Immortaldb
🔩 A relentless key-value store for the browser.
Stars: ✭ 2,962 (+4387.88%)
Mutual labels:  storage, localstorage, sessionstorage
h5webstorage
Web Storage for Angular 2
Stars: ✭ 38 (-42.42%)
Mutual labels:  storage, localstorage, sessionstorage
Vue Ls
💥 Vue plugin for work with local storage, session storage and memory storage from Vue context
Stars: ✭ 468 (+609.09%)
Mutual labels:  storage, localstorage, sessionstorage
React Storage Hooks
React hooks for persistent state
Stars: ✭ 146 (+121.21%)
Mutual labels:  storage, localstorage, sessionstorage
ng2-storage
A local and session storage wrapper for angular 2.
Stars: ✭ 14 (-78.79%)
Mutual labels:  storage, localstorage, sessionstorage
Proxy Storage
Provides an adapter for storage mechanisms (cookies, localStorage, sessionStorage, memoryStorage) and implements the Web Storage interface
Stars: ✭ 10 (-84.85%)
Mutual labels:  storage, localstorage, sessionstorage
phaser-super-storage
A cross platform storage plugin for Phaser
Stars: ✭ 49 (-25.76%)
Mutual labels:  storage, localstorage
client-persist
Offline storage for your web client. Supports IndexedDB, WebSQL, localStorage and sessionStorage with an easy to crawl with API.
Stars: ✭ 14 (-78.79%)
Mutual labels:  localstorage, sessionstorage
localstorage-manager
LocalStorage Manager is a Chrome extension to add, edit, remove, export and import local storage and session storage data
Stars: ✭ 37 (-43.94%)
Mutual labels:  localstorage, sessionstorage
svelte-persistent-store
A Svelte store that keep its value through pages and reloads
Stars: ✭ 111 (+68.18%)
Mutual labels:  localstorage, sessionstorage
elm-storage
Unified interface for accessing and modifying LocalStorage, SessionStorage and Cookies
Stars: ✭ 13 (-80.3%)
Mutual labels:  localstorage, sessionstorage
ddrive
A lightweight cloud storage system using discord as storage device written in nodejs
Stars: ✭ 25 (-62.12%)
Mutual labels:  storage, localstorage
nativestor
NativeStor provide kubernetes local storage which is light weight and high performance
Stars: ✭ 20 (-69.7%)
Mutual labels:  storage, localstorage
Ng2 Webstorage
Localstorage and sessionstorage manager - Angular service
Stars: ✭ 395 (+498.48%)
Mutual labels:  localstorage, sessionstorage

Recoil Persist

Tiny module for recoil to store and sync state to Storage. It is only 354 bytes (minified and gzipped). No dependencies. Size Limit controls the size.

Demo

If you are using recoil-persist with version 1.x.x please check migration guide to version 2.x.x.

Example of persist state in localStorage

import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
import { atom, RecoilRoot, useRecoilState } from 'recoil'
import { recoilPersist } from 'recoil-persist'

const { persistAtom } = recoilPersist()

const counterState = atom({
  key: 'count',
  default: 0,
  effects_UNSTABLE: [persistAtom],
})

function App() {
  const [count, setCount] = useRecoilState(counterState)
  return (
    <div>
      <h3>Counter: {count}</h3>
      <button onClick={() => setCount(count + 1)}>Increase</button>
      <button onClick={() => setCount(count - 1)}>Decrease</button>
    </div>
  )
}

ReactDOM.render(
  <React.StrictMode>
    <RecoilRoot>
      <App />
    </RecoilRoot>
  </React.StrictMode>,
  document.getElementById('root'),
)

Install

npm install recoil-persist

or

yarn add recoil-persist

Now you could add persisting a state to your app:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import { RecoilRoot } from "recoil";
+import { recoilPersist } from 'recoil-persist'

+const { persistAtom } = recoilPersist()

const counterState = atom({
  key: 'count',
  default: 0,
+ effects_UNSTABLE: [persistAtom],
})

function App() {
  const [count, setCount] = useRecoilState(counterState)
  return (
    <div>
      <h3>Counter: {count}</h3>
      <button onClick={() => setCount(count + 1)}>Increase</button>
      <button onClick={() => setCount(count - 1)}>Decrease</button>
    </div>
  )
}

ReactDOM.render(
  <React.StrictMode>
    <RecoilRoot>
      <App />
    </RecoilRoot>
  </React.StrictMode>,
  document.getElementById('root'),
)

After this each changes in atom will be store and sync to localStorage.

Usage

import { recoilPersist } from 'recoil-persist'

const { persistAtom } = recoilPersist({
  key: 'recoil-persist', // this key is using to store data in local storage
  storage: localStorage, // configurate which stroage will be used to store the data
})

Example of persist state in localStorage

API

recoilPersist(config)

config parameter

type config.key = String

Default value of config.key is recoil-persist. This key is using to store data in storage.

type config.storage = Storage

Set config.storage with sessionStorage or other Storage implementation to change storage target. Otherwise localStorage is used (default).

Migration from version 1.x.x to 2.x.x

The API changed from version 1.x.x.

To update your code just use this migration guide:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import { RecoilRoot } from "recoil";
import { recoilPersist } from 'recoil-persist' // import stay the same

const {
-  RecoilPersist,
-  updateState
+  persistAtom
} = recoilPersist(
-   ['count'], // no need for specifying atoms keys
    {
        key: 'recoil-persist', // configuration stay the same too
        storage: localStorage
    }
)

const counterState = atom({
  key: 'count',
  default: 0,
- persistence_UNSTABLE: { // Please remove persistence_UNSTABLE from atom definition
-   type: 'log',
- },
+ effects_UNSTABLE: [persistAtom], // Please add effects_UNSTABLE key to atom definition
})

function App() {
  const [count, setCount] = useRecoilState(counterState)
  return (
    <div>
      <h3>Counter: {count}</h3>
      <button onClick={() => setCount(count + 1)}>Increase</button>
      <button onClick={() => setCount(count - 1)}>Decrease</button>
    </div>
  )
}

ReactDOM.render(
  <React.StrictMode>
-   <RecoilRoot initializeState={({set}) => updateState({set})>
+   <RecoilRoot> // Please remove updateState function from initiallizeState
-     <RecoilPersist /> // and also remove RecoilPersist component
      <App />
    </RecoilRoot>
  </React.StrictMode>,
  document.getElementById('root')
);

Demo

$ git clone [email protected]:polemius/recoil-persist.git
$ cd recoil-persist
$ npm install
$ npm run start

Please open localhost:1234.

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].