Monday, 3 October 2016

reactjs - mapDispatchToProps with react-redux' connect() and class components



I'm following relatively closely the tutorial of react-redux but decided to write my presentational component as a class. As you can see in the following code I wrote a submitHandler which should call the submitHandler mapped by react-redux' connect(). But when I call it, it results in TypeError: Cannot read property 'props' of null.



Any suggestions?



Container Component






import { connect } from 'react-redux'
import New from './New'
import { newEntry } from '../../store/actions'

function mapStateToProps(state) {
return {
value: state.currentEntries[state.currentEntries.length - 1].value,
min: state.currentEntries[state.currentEntries.length - 1].value,
max: 148
}

}

function mapDispatchToProps(dispatch) {
return {
onSubmit: (date, value) => {
dispatch(newEntry(date, value))
}
}
}


const NewContainer = connect(
mapStateToProps,
mapDispatchToProps
)(New)

export default NewContainer






Presentational Component





import React from 'react'
import moment from 'moment'

export default class Chart extends React.Component {
submitHandler(e) {
e.preventDefault()


// BUG: Results in "TypeError: Cannot read property 'props' of null"
this.props.onSubmit(this.date.value, this.number.value)

this.date.value = moment().format('YYYYY-MM-DD')
this.number.value = this.props.min
}

render() {
const { value, min, max } = this.props


return (

{ this.date = node}} defaultValue={moment().format('YYYYY-MM-DD')} />
{ this.number = node }} min={min} max={max} defaultValue={value} />


)
}
}





Answer



You just need to bind your submit handler:



 



Should be:




 


Or:



  this.submitHandler(e)}>


The difference with a class component is that methods do not auto-bind this.



No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...