react-redux使用redux处理多个数据

时间:2018-11-02 01:52:09

标签: javascript react-native redux

我正在尝试使用redux设置全局状态,当我尝试传递单个数据时它可以工作,但是当我尝试传递多个数据时却不能工作。下面是我的代码:

<CSButton
           onPress={() => {
                          this.setState({
                            comment   : this.state.comment,
                            region  : this.state.region,            
                           },
                    () => this.props.commentHandler(this.state),
                          this.props.regionHandler(this.state), 
           // I am getting correct answer when I console.log here         
                    () => console.log(this.props.comment,'this.props.comment?'),
                    () => console.log(this.props.region,'this.props.region?'))}}>
           <Text>Button</Text>
          </CSButton>

//when I try to console.log using another button, works fine for 'this.props.comment'

           <Button
                    title='comment'
                    onPress={()=> console.log(this.props.comment,'comment')}>
          </Button>

    //But when I try to console.log `this.props.region` it gives me undefined

          <Button
                title='region'
                onPress={()=> console.log(this.props.region,'region')}>
          </Button>

function mapStateToProps(state) {
    return {
        region   : state.region,
        comment  : state.comment,
    }
}

function mapDispatchToProps(dispatch) {
    return {
        regionHandler  : (state) => dispatch({ type: 'REGION', payload: state.region}),
        commentHandler : (state) => dispatch({ type: 'COMMENT', payload: state.comment}),

    }
}

App.js

const initialState = {
    comment:'',
    region:[],
}

const reducer = (state = initialState, action) => {
  console.log(action);
    switch (action.type)
    {
        case 'COMMENT':
            return { comment: action.payload}
        case 'REGION':
            return { region: action.payload}
    }
    return state
}

const store = createStore(reducer)

似乎我的代码仅调用第一个处理程序this.props.commentHandler(this.state),而不调用第二个处理程序this.props.regionHandler(this.state)

是否可以解决此问题?任何意见或评论将不胜感激!

2 个答案:

答案 0 :(得分:1)

this.setState(partialState, callback)仅采用一个回调函数。您将在此处传递两个函数:

 () => this.props.commentHandler(this.state),
       this.props.regionHandler(this.state), 

代替尝试:

() => {
  this.props.commentHandler(this.state)
  this.props.regionHandler(this.state)
}

答案 1 :(得分:1)

您已将初始状态分配给状态state = initialState,但从未使用过。每次触发操作时,都会向视图发送一个新对象。您必须将其设置为状态。

尝试一下。您必须以一成不变的方式执行此操作。

   const reducer = (state = initialState, action) => {
        switch (action.type)
        {
            case 'COMMENT':
                state = {
                    ...state,
                    comment: action.payload,
                }
                break;
            case 'REGION':
                state = {
                    ...state,
                    region: action.payload,
                }
                break;
        }
        return state
    }

我只是注意到您在代码中错过了breaks;

如果您对不可变状态树有疑问。请参阅此免费视频系列。 Link