LoginSignup
33
30

More than 5 years have passed since last update.

Redux: state を local storage に保存する (redux-localstorage)

Posted at

概要

Redux で state を local storage に保存する middleware redux-localstorage の紹介です。

インストール

npm install --save redux-localstorage

使い方

middleware として persistState を追加します。

store/configureStore.js
import persistState from 'redux-localstorage'

export const finalCreateStore = compose(
  persistState("auth"),
)(createStore);

引数には保存する state 名を指定します。
ルートのみ指定可能で、保存は指定したルート以下をすべて保存します。
state を指定しない場合はすべての state を保存します。

複数の state を保存したい場合は配列で指定します。

  persistState(["auth", "my"]),

local storge にデータがある場合は、次のアクセス時にその state で再開されます。

参考

33
30
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
33
30