如何在reducer中更新状态

时间:2017-12-18 00:58:06

标签: react-native react-redux

我的减速机有问题。我正在使用redux创建一个登录页面。我已成功登录但未能导航到下一页。我的reducer中的状态未更新。我该如何解决这个问题?

这就是我写减速器的方法:

import { LOGIN_SUCCESS } from '../actions/types';

const INITIAL_STATE={
  isLoginSuccess:false,
}
export default function (state=INITIAL_STATE, action){
switch(action.type){
  case LOGIN_SUCCESS:
   return {
     isLoginSuccess : true
   }

  default:
    return INITIAL_STATE;
 }
}

这是我编写动作的方式:

import axios from 'axios';
import * as helper from '../common';
import { LOGIN_SUCCESS } from './types';

export const attemptLogin = (username, password) => async dispatch => {

let param = {
    txtNomatrik: username,
    txtPwd: password,
    public_key: helper.PUBLIC_KEY,
    secret_key: helper.SECRET_KEY

}

console.log(`${helper.ROOT_API_URL}/v1/basic/ad/std/login`)
let login_res = await 
axios.post(`${helper.ROOT_API_URL}/v1/basic/ad/std/login`, param)

console.log(login_res.data);

if (login_res.data.status == 'Successful Login') {
    const { login } = login_res.data;
    await AsyncStorage.seItem('Login_token', username);
    await AsyncStorage.setItem('profile', JSON.stringify(login));
    dispatch({ type: LOGIN_SUCCESS, payload: { isLoginSuccess : true } });

}
}

我想在我的索引文件中使用isLoginSuccess导航登录到下一页,如下所示:

componentWillReceiveProps(nextProps){
    if(nextProps.isLoginSuccess){
        this.props.navigation.navigate('logged');
    }

}

以下是我连接redux的方法:

const mapStateToProps = ({ auth }) => {
return {
    isLoginSuccess: auth.isLoginSuccess
}
}
export default connect(mapStateToProps, actions)(LoginScreen);

以下是我的combineReducer文件:

import {combineReducers} from 'redux';
import news from './welcome_reducer';
import auth from './login_reducer';

export default combineReducers({
news,
auth
})

我该如何解决这个问题?我现在感到迷茫,已经尝试了很多方法来解决它。登录的Api调用成功,但根本不调度该操作。我无法更新状态,因此无法导航到下一页。请帮帮我

0 个答案:

没有答案