1

I have something like this MobX store and would like to add state persistence to it using the library mobx-persist https://github.com/pinqy520/mobx-persist But I can't figure out how to do it right

My store:

import { observable, makeObservable, action, runInAction } from 'mobx'
import Api from './Api'

export function userStore() {
    return makeObservable({
        isAuthenticated: false,
        token: '',
        async login() {
            const api = new Api()
            let tokenStr = await api.getToken(encodeURIComponent('xxx'), encodeURIComponent('yyy'))
            runInAction(() => {
                this.token = tokenStr
            })
            this.setAuthenticated(this.token)
        },
        setAuthenticated(tokenValue) {
            this.isAuthenticated = tokenValue != '' ? true : false
        }
    },{
        isAuthenticated: observable,
        token: observable,
        login: action.bound,
        setAuthenticated: action.bound
    })
}

I want to keep both fields:

  • isAuthenticated
  • token
Alex Muha
  • 11
  • 3

0 Answers0