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

Constructor - calling super and bind #74

Closed
kkarkos opened this issue Jun 9, 2016 · 3 comments
Closed

Constructor - calling super and bind #74

kkarkos opened this issue Jun 9, 2016 · 3 comments

Comments

@kkarkos
Copy link

kkarkos commented Jun 9, 2016

Hi @jaredpalmer , started working on a project using your starter-project. Great work!

I have a question about dispatching redux actions on user interactions (e.g. button click). My questions is more about the right syntax. In the past I called the constructor and called the bind function like that:

function mapDispatchToProps(dispatch) {
    return bindActionCreators({
        importedAction,
    }, dispatch);
}
class Calendar extends React.Component {

constructor(props) {
   super(props);
   this.handleClick = this.handleClick.bind(this);
}

handleClick(programId) {
        this.props.importedAction()
        .then((response) => {
            do something with response
        });
    }
 }
....

render() {
 return (
   <a onClick={this.handleClick}>Click me to dispatch action</a>
)
}

I think you have a babel-plugin installed which allows you to avoid writing the "extends React.Component" all the time. I cant see 'bindActionCreators' as well.

Question, how would I write above in the way it could be used in your starter project?

@wedneyyuri
Copy link
Contributor

function mapDispatchToProps(dispatch) {
    return bindActionCreators({
        importedAction,
    }, dispatch);
}
class Calendar extends React.Component {

handleClick(programId) {
        this.props.importedAction()
        .then((response) => {
            do something with response
        });
    }
 }
....

render() {
 return (
   <a onClick={(evt) => this.handleClick()}>Click me to dispatch action</a>
)
}

@wedneyyuri
Copy link
Contributor

Seems like you are not using connect from react-redux:

import { connect } from 'react-redux'

... your component...

export default connect(mapStateToProps, actionCreators)(Calendar)

@jaredpalmer
Copy link
Owner

Closing this because it's not related to the boilerplate. However, I suggest watching Dan Abramov's courses on Redux:

Also check out the react-redux API for connect: https://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options

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

3 participants