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

ReferenceError: window is not defined #29

Closed
MBach opened this issue Oct 6, 2017 · 2 comments
Closed

ReferenceError: window is not defined #29

MBach opened this issue Oct 6, 2017 · 2 comments

Comments

@MBach
Copy link

MBach commented Oct 6, 2017

When using mobx-persist with SSR, I have these messages (printed like 10 times) both in dev or in production mode:

ReferenceError: window is not defined
at /home/www/my-project/node_modules/mobx-persist/lib/storage.js:18:45

How can I suppress them? Should window and localstorage be polyfilled?

@pinqy520
Copy link
Owner

pinqy520 commented Oct 7, 2017

import { create } from 'mobx-persist'
if (isBrowser /* or !isServer */) {
    const hydrate = create(...)
    hydrate('yourStoreKey', yourStore, yourStateFromServer)
}

@MBach
Copy link
Author

MBach commented Oct 9, 2017

Thanks, it's working! Maybe the doc can be completed with your code snippet?

Here's what I used in my project:

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

class User {
    @observable hydrated = false
    @persist('object') @observable _content = null
    @action hydrateComplete() {
        this.hydrated = true
    }

    ...

}

let userStore = new User()
export default userStore

if (process.browser) {
    const hydrate = create({ jsonify: true })
    hydrate('user', userStore).then(() => userStore.hydrateComplete())
}

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