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

Integration with custom App component #80

Closed
tlenclos opened this issue Apr 24, 2018 · 6 comments
Closed

Integration with custom App component #80

tlenclos opened this issue Apr 24, 2018 · 6 comments

Comments

@tlenclos
Copy link

tlenclos commented Apr 24, 2018

Hi and thanks for your wrapper !

I'm using it fine on each page of my application. I recently updated my next app with v6 to extend the app component (see vercel/next.js#4129) and was wondering if maybe we could use this new extension point to improve the integration of the wrapper.

This could be great if instead of repeating this code we could have a standard React Redux connect for the pages.

export default withRedux(initStore, mapStateToProps, mapDispatchToProps)(MyPage);
@tlenclos
Copy link
Author

tlenclos commented Apr 24, 2018

Moving a bit further I managed to get rid of the wrapper just by using a custom _app.js component.

function getOrCreateStore(initialState = {}, req) {
  let isBrowser = typeof window !== 'undefined';
  let store = null;

  if (isBrowser) {
    // Memoize store on client
    if (!window['reduxStore']) {
      store = initStore(initialState, {});
      window['reduxStore'] = store;
    } else {
      store = window['reduxStore'];
    }
  } else {
    // Server store
    store = initStore(initialState, { req });
  }

  return store;
}

export default class MyApp extends App {
  static async getInitialProps({ Component, router, ctx }) {
    let pageProps = {};

    // Redux store
    let store = getOrCreateStore(ctx.req);
    ctx.store = store;

    // Page props
    if (Component.getInitialProps) {
      pageProps = await Component.getInitialProps(ctx);
    }

    return { pageProps, store, initialState: store.getState() };
  }

  render() {
    const { Component, pageProps, initialState } = this.props;

    return (
      <Container>
        <Provider store={store && store.dispatch ? store : getOrCreateStore(initialState)}>
           <Component {...pageProps} />
        </Provider>
      </Container>
    );
  }
}

@kirill-konshin
Copy link
Owner

Thanks for your interest and recommendation, I will release a new version for 6.x soon.

@kirill-konshin
Copy link
Owner

kirill-konshin commented Apr 24, 2018

@kirill-konshin
Copy link
Owner

Released 2.0.0-beta.2 (tag canary).

@tlenclos
Copy link
Author

That was fast, thanks 👍
Everything working good for me with 2.0.0-beta.2.

@kirill-konshin
Copy link
Owner

Awesome. I will keep it in canary and beta until NextJS 6.x will be released.

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