Recoil/개념정리

[RECOIL] 로컬 스토리지 저장방법

xowoony 2023. 4. 25. 12:05

학습일 : 2023. 04. 25


 https://www.npmjs.com/package/recoil-persist

 

recoil-persist

Package for recoil to persist and rehydrate store. Latest version: 4.2.0, last published: a year ago. Start using recoil-persist in your project by running `npm i recoil-persist`. There are 4 other projects in the npm registry using recoil-persist.

www.npmjs.com

 

 

 

사용을 위해 install

npm i recoil-persist

 

 

import

import { recoilPersist } from 'recoil-persist'

 

 

 

<atom.tsx>

atom 에

const { persistAtom } = recoilPersist();

effects_UNSTABLE: [persistAtom]

추가해주면 된다.

// recoil persist - 로컬스토리지 저장
const { persistAtom } = recoilPersist();

// toDoState
export const toDoState = atom<IToDo[]>({
  key: "toDo",
  default: [],
  effects_UNSTABLE: [persistAtom],
});