我是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中起作用。怎么了?预先感谢!
import {bindActionCreators} from 'redux';
function mapDispatchToProps=(dispatch)=> {
return {
actions: {bindActionCreators({setusername,setstatuscursa,dispatch})}
}
}
现在您可以将您的操作派遣为:
this.props.actions.setusername(user)
this.props.actions.setstatuscursa(val)