react-native - 从商店 redux 获取状态值

标签 react-native react-redux

我怎样才能从商店中获得状态的值(value)?

我已经尝试使用 store.getState().isLoggedIn 但它没有用:

import * as actions from 'actions/actions'
import store from 'store/store'

store.dispatch(actions.login(this.state.username,this.state.password)).then(() => {
        state = store.getState();
          alert(state.isLoggedIn);
      });

我正在尝试获取状态“isLoggedIn”的值,但它返回“undefined”。如何从商店正确获取状态值?

这是我的源代码

reducer :
const INITIAL_STATE={
  isLoggedIn:false,
  isLoading:false,
  userData:{},
  error:undefined
}

export default function auth(state=INITIAL_STATE,action){
  console.log(action.type);
  switch (action.type) {
    case 'LOGIN_ATTEMPT':
      return{
        ...state,
        isLoading:true,
        isLoggedIn:false
      }
      break;
    case 'LOGIN_SUCCESS':
    return {
        ...state, 
        isLoading:false,
        isLoggedIn:true,
        userData:action.userData,
        error:undefined
      }
      break;
    case 'LOGIN_FAILED':
      return{
        ...state,
        isLoading:false,
        isLoggedIn:false,
        error:action.error
      }
      break;
    case 'LOGOUT':
      return{
        ...state,
        isLoading:false,
        isLoggedIn:false
      }
      break;
    default:
      return state
  }
}

行动:
export function isLoading(bool:Boolean){
  return{
    type:'LOGIN_ATTEMPT',
    isLoading:bool
  }
}

export function loginSuccess(userData:Object){
  return{
    type:'LOGIN_SUCCESS',
    userData
  }
}

export function loginFailed(error:Object){
  return{
    type:'LOGIN_FAILED',
    error
  }
}

export function login(uname,pass){
  return dispatch => {
    dispatch(isLoading(true));
    return fetch('http://192.168.99.1:5000/user/login',{
      method:'POST',
      headers:{
        'Content-Type':'application/json'
      },
      body:JSON.stringify({
        username:uname
      })
    })
    .then((response) => {
      if(response.status < 300){
        dispatch(isLoading(false))
        response.json().then((responseJSON) => {
          if(responseJSON['message'] == "True" && bcrypt.compareSync(pass, responseJSON['hash']) ){
            console.log("responseJSON",responseJSON);
            dispatch(loginSuccess(responseJSON));
          }
          else{
            dispatch(isLoading(false))
            dispatch(loginFailed(responseJSON.message))
          }
        })
      }
      else{
        response.json().then((responseJSON) => {
          console.log("responseJSON",responseJSON);
          dispatch(isLoading(false))
          dispatch(loginFailed(responseJSON.message))
        })
      }
    })
    .catch((error) => {
      console.log("error",error);
      dispatch(isLoading(false))
      dispatch(loginFailed(error))
    })
  }
}

店铺:
import {combineReducers} from 'redux'
import auth from './authReducer'
import {createStore,applyMiddleware} from 'redux'
import thunk from 'redux-thunk';

const rootReducer = combineReducers({
  auth
})

let store = createStore(rootReducer,applyMiddleware(thunk))

export default store;

最佳答案

我能够使用 JSON.stringify 查看数据

  JSON.stringify(store.getState())

它返回json数据。使用此方法可以轻松查看对象

关于react-native - 从商店 redux 获取状态值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56352230/

相关文章:

android - 无效的资源目录名称,drawable-anydpi-v21

react-native - 如何在 componentDidMount 上更改 React Native 中的状态?

reactjs - OnClick 在使用 React JS 和 webpack 的移动设备上无法正常工作

reactjs - React Native fetch 不适用于 redux?

javascript - 从完整日期获取时的时间错误

image - 为 React Native 应用程序选择图像大小

react-native - 在 React Native 中使用多行自动增长文本输入

javascript - react redux项目报错如何解决?

javascript - 在 mapDispatchToProps 中传递事件对象和参数

redux - 如果 redux 中的状态应该是不可变的,我将如何与有状态 API 交互?