簡體   English   中英

處理多個數據時反應native-redux出錯

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

我正在使用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)但它不能一起工作,則效果很好。

知道我做錯了什么嗎? 任何意見或建議,將不勝感激!

將我的處理程序包裝為一個函數即可。 感謝@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