I am using react native-redux to set global state and it works fine when handling single data but when I have multiple data its not working. Below is my code:
import {connect} from 'react-redux'
constructor(props){
super(props)
this.state = {
comment:'',
region:[],
}
}
<Button
onPress={() => {
this.setState({
comment : this.state.comment,
// works fine if I only handle comment or region
region : this.state.region,
},
() => this.props.commentHandler(this.state),
() => this.props.regionHandler(this.state)}}>
<Text>UPDATE</Text>
</Button>
function mapStateToProps(state) {
return {
comment : state.comment,
region : state.region
}
}
function mapDispatchToProps(dispatch) {
return {
commentHandler : (state) => dispatch({ type: 'COMMENT', payload: state.comment}),
regionHandler : (state) => dispatch({ type: 'REGION', payload: state.region})
}
}
I don't know what I did wrong but when I try to handle multiple data for example, comment & region it won't work. It works perfectly if I remove () => this.props.commentHandler(this.state)
or () => this.props.regionHandler(this.state)
but doesn't work together.
Any idea what I might be doing wrong? Any comments or advise would be really appreciated!
Wrapping my handlers into one function made it work. Thanks to @Paulquappe.
<Button
onPress={() => {
this.setState({
comment : this.state.comment,
region : this.state.region,
},
() => this.props.regionHandler(this.state),this.props.commentHandler(this.state),
() => console.log(this.props.comment,'this.props.comment?'),
() => console.log(this.props.region,'this.props.region?'))
}}>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.