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

Added next-redux-wrapper to example #1196

Merged
merged 2 commits into from Feb 18, 2017
Merged

Conversation

kirill-konshin
Copy link
Contributor

First of all, thank for great software!

I have made it a bit easier for developers to integrate Redux with Next.js, basically, I reworked your example and made a package https://github.com/kirill-konshin/next-redux-wrapper that hides all the underlying complexity from end users.

I thought it would be a bad idea to drag redux-related dependencies to main next package, that's why it's a separate package.

The purpose of this PR: I have simplified the example using this component.

import { reducer, initStore, startClock } from '../store'
import wrapper from 'next-redux-wrapper';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe call it withRedux ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name of the function could be any because it's a default export, it my code it does not even have a name :) but yes, makes sense.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep

@rauchg
Copy link
Member

rauchg commented Feb 18, 2017

Awesome. Please explain in the README what next-redux-wrapper is and what it does? So that it doesn't seem like a lot of magic is at play

@kirill-konshin
Copy link
Contributor Author

kirill-konshin commented Feb 18, 2017

Done, there's no magic -- it auto-creates store when getInitialProps is called and then passes it down to Provider, also automatically. On the client side it also takes care of using same store every time.

@rauchg rauchg merged commit f0cb5b7 into vercel:master Feb 18, 2017
@rauchg
Copy link
Member

rauchg commented Feb 18, 2017

Thank you @kirill-konshin ! cc @impronunciable

@impronunciable
Copy link
Contributor

Awesome sauce! I'll take a look

@amitsch666
Copy link

amitsch666 commented Aug 12, 2017

Stupid question for @kirill-konshin and other experts here: How do I access the store outside of getInitialProps()? Say I want to dispatch an action from my onSubmit() method?

I am trying to call the dispatch method from within my onSubmit() like so:

onSubmit(e) {
    e.preventDefault();
    axios.post('/api/authentication/login', { username: this.state.username, password: this.state.password })
      .then(function (response) {
        console.log(response);
        console.log('First: ', response.data.firstName);
				// dispatch user data to the store
				this.props.testclick(response.data.email);
      })
      .catch(function (error) {
        console.log(error);
      });
  }

And my mapDispatchToProps() is defined:

const mapDispatchToProps = (dispatch) => {
	return {
		testclick: (currUser) => dispatch({ type: 'FOO', payload: currUser })
	}
}

However, when I run it, the browser throws an error on this line saying this is undefined:

this.props.testclick(response.data.email);

Any workaround?

@amitsch666
Copy link

Never mind, a simple self = this resolved the issue. Silly me!

@lock lock bot locked as resolved and limited conversation to collaborators May 12, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants