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

Client kept initialState #27

Closed
DCKT opened this issue May 31, 2017 · 2 comments
Closed

Client kept initialState #27

DCKT opened this issue May 31, 2017 · 2 comments

Comments

@DCKT
Copy link

DCKT commented May 31, 2017

Hello,

In the example, I found something strange (correct me if I'm wrong) about the state in client after calling getInitialProps in _document from the server.

We dispatch an async action on server to edit tack

// _document.js
static async getInitialProps(ctx) {

        const props = await Document.getInitialProps(ctx);
        const {store, isServer, pathname, query} = ctx;

        console.log(MyDocument.name, '- 2. Cmp.getInitialProps uses the store to dispatch things, pathname', pathname, 'query', query);

        if (isServer) {

            return new Promise((res) => {
                setTimeout(() => {
                    store.dispatch({type: 'TACK', payload: 'server'}); // <--- here
                    res(props);
                }, 200);
            });

        }

        store.dispatch({type: 'TACK', payload: 'client'});

        return props;

    }

So in the tack should be equal to server but it's equal to init.

// index.js
...
render () {
  return  (
    <div>Redux tack: {this.props.tack} (_document)</div>
  )
}

Thanks for your help.

@kirill-konshin
Copy link
Owner

It seems that you're hitting the known issue vercel/next.js#1267. Per Next.js authors it's better NOT to use _document for that purpose, use HOC instead.

@kirill-konshin
Copy link
Owner

Closing because of no activity

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