首页 > 解决方案 > 处理多个数据时反应 native-redux 出错

问题描述

我正在使用 react native-redux 来设置全局状态,它在处理单个数据时工作正常,但是当我有多个数据时它不起作用。下面是我的代码:

 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})

        }
    }

我不知道我做错了什么,但是当我尝试处理多个数据时,例如,评论和区域它将不起作用。如果我删除 () => this.props.commentHandler(this.state)() => this.props.regionHandler(this.state)不一起工作,它会完美工作。

知道我可能做错了什么吗?任何意见或建议将不胜感激!

标签: javascriptreact-nativereact-redux

解决方案


将我的处理程序包装到一个函数中使其工作。感谢@Paulquape。

  <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?'))
                     }}>

推荐阅读