简体   繁体   English

处理多个数据时反应native-redux出错

[英]react native-redux getting error when handling multiple data

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. 我正在使用react native-redux设置全局状态,并且在处理单个数据时可以正常工作,但是当我有多个数据时它无法正常工作。 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. 如果我删除() => this.props.commentHandler(this.state)() => this.props.regionHandler(this.state)但它不能一起工作,则效果很好。

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. 感谢@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?'))
                     }}>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM