安装
c
npm i valtio
//好用的storage
npm i good-storage
使用
src目录下创建store文件夹,新增两个文件
index.js
js
export * from './user'
user.js
js
import { proxy,subscribe } from 'valtio'
import {useProxy} from 'valtio/utils'
import ss from 'good-storage'
const key = "USER_STORE"
const state = proxy(ss.get(key) || {
token:'',
info:{}
})
//持久化
subscribe(state, () =>{
ss.set(key,state)
})
export function useUserStore(){
return useProxy(state)
}
export const userActions = {
setToken:async _token=>{
state.token = _token
await userActions.updateUserInfo()
return state.info
},
setInfo:_info=>state.info = _info,
}