Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot get mobx persist to work without decorators. Simple example #87

Open
Danscap opened this issue Aug 4, 2020 · 1 comment
Open

Comments

@Danscap
Copy link

Danscap commented Aug 4, 2020

I have a userStore class below.

import {observable, action, decorate, when, computed} from 'mobx'


import { requests, Scripts } from 'axios.js'

import { create, persist } from 'mobx-persist'

import userAltImage from 'images/user.png'


const hydrate = create({
  storage: localStorage,
  jsonify: false,
});

class UserStore {

        // scriptsPerRequest = 100
        // less_or_more = 'first'

        constructor(){
        }
        avatar = {
                file: userAltImage,
                width: '50px',
                height: '50px'

        }

        scripts = {
                self: []
        }

        setAvatar(file,width, height){
                this.avatar.file = file
                this.avatar.width = width

                this.avatar.height = height

        }

        // loadSelfScripts(){
        //      Scripts.byAuthor(1, this.scriptsPerRequest, this.less_or_more,)
        // }



}

decorate(UserStore,{
        avatar: [persist('object'), observable],
        scripts: [persist('object'), observable],
        setAvatar: action
})




var userStore = new UserStore()
hydrate('user', userStore ).then( () => console.log('userStore hydrated!'))


export default userStore

Later in a different componentX.js

I have this code

handledata = e =>{
    .....
    this.props.userStore.setAvatar(new_image_data, '50px', '50px')
}

When this runs, mobx works correctly and the userStore Avatar object has been successfully updated. But when I refresh the page, the data is lost... indicating that mobx persist is not working to persist the store even though the console says 'userStore hydrated' after the hydrate function runs.

What am I doing wrong? Please advise. I cannot get this to work.

@gastonyelmini
Copy link

gastonyelmini commented Nov 7, 2020

@Danscap same issue, until I changed jsonify: false, to jsonify: true,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants