【问题标题】:React-Native ReduxReact-Native Redux
【发布时间】:2019-02-28 10:41:21
【问题描述】:

我是 React-Native 中 Redux 的新手,我需要帮助才能采取行动。这是我的 reducer.js:

const initialState = {
    redusername: '',
    redstatuscursa: '',
}

const reducer = (state = initialState, action) => {
    switch (action.type) {
        case 'SETUSERNAME':
            return {  ...state, redusername: action.payload }
        case 'SETSTATUSCURSA':
            return {  ...state, redstatuscursa: action.payload }    
    }
    return state
  }

export default reducer;

这是在 Main.js 文件中:

function mapStateToProps(state) {
    return {
    redusername: state.redusername,
    redstatuscursa: state.redstatuscursa
    }
}

function mapDispatchToProps(dispatch) {
    return {
    setusername: (user) => dispatch({ type: 'SETUSERNAME', payload: user }),
    setstatuscursa: (val) => dispatch({ type: 'SETSTATUSCURSA', payload: val })    
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(Main)

当我调用 this.props.setusername(this.state.username);在一个函数中,用户名被转移到存储,但是当我调用 this.props.setstatuscursa("OK");在 componentDidMount() redstatuscursa 中不要改变。但最后一个电话在 onPress 中有效。怎么了?提前致谢!

【问题讨论】:

    标签: react-native redux


    【解决方案1】:
       import {bindActionCreators} from 'redux';
    
       function mapDispatchToProps=(dispatch)=> {
        return {
        actions: {bindActionCreators({setusername,setstatuscursa,dispatch})}  
        }
    }
    

    现在您可以将您的操作发送为:

    this.props.actions.setusername(user)
    this.props.actions.setstatuscursa(val)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-16
      • 2017-11-28
      • 1970-01-01
      • 2021-10-23
      • 2018-07-24
      • 2017-05-17
      • 2018-05-23
      • 2021-08-23
      相关资源
      最近更新 更多