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

Feature Request: Make mapped props available in getInitialProps #48

Closed
michaeljonathanblack opened this issue Aug 29, 2017 · 1 comment

Comments

@michaeljonathanblack
Copy link

michaeljonathanblack commented Aug 29, 2017

I wish there was a clean way to have bound action creators in getInitialProps. Right now, when passing in mapDispatchToProps to withRedux, these actions have to be created and dispatched imperatively with the store object, e.g.

  static async getInitialProps({ store }) {
   store.dispatch(plusCount());
  }

Preferably, that'd look something like

  static async getInitialProps({ props }) {
    props.plusCount();
  }
const mapDispatchToProps = dispatch =>
  bindActionCreators({ plusCount }, dispatch);

export default withRedux(makeStore, null, mapDispatchToProps)(MyPage);

Unfortunately this pattern requires calling this function again inside getInitialProps, breaking the no-use-before-define rule.

  static async getInitialProps({ store }) {
    const props = mapDispatchToProps(store.dispatch);
    props.plusCount();
  }

Given how weird that seems, and that those actions aren't often called outside of the getInitialProps method, it seems better to write a custom function that's more descriptive, and use an actions object in getInitialProps.

const getInitialActions = store =>
  bindActionCreators({ getMediaItems, plusCount }, store.dispatch);
  static async getInitialProps({ store }) {
    const actions = getInitialActions(store);
    actions.plusCount();
  }

It'd be nice if next-redux-wrapper would pick up mapStateToProps and mapDispatchToProps and expose those props to getInitialProps.

If that seems a little awkward and potentially hazardous, exposing an actions object when mapDispatchToProps is supplied is a less ambitious solution.

@michaeljonathanblack michaeljonathanblack changed the title Feature Request: Make props available in getInitialProps Feature Request: Make mapped props available in getInitialProps Aug 29, 2017
@kirill-konshin
Copy link
Owner

I'm not sure it's possible to get own props in getInitialProps. Also I think that wrapping is not really necessary, because you can dispatch actions in the way you've shown and it's cleaner and more obvious than through wrappers.

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