动作不会将数据调度到reducer

时间:2018-10-28 18:00:34

标签: react-native react-redux expo redux-thunk

我正在尝试将操作中获取的数据发送到save_user_reducer,但它似乎不起作用。

在我的控制台日志中,我可以看到ID已被获取,但是数据并未通过dispatch命令传递。

我的身份验证操作:

    import {AsyncStorage} from 'react-native';
import {Facebook} from 'expo';

import{
  FACEBOOK_LOGIN_SUCCESS,
  FACEBOOK_LOGIN_FAIL,
  TOKEN_RECEIVED,
  SAVE_USER
} from './types';


export const tryGetFacebookToken = () => async dispatch => {
  let token = await AsyncStorage.getItem('fb_token');
  let response = await fetch(`https://graph.facebook.com/me?access_token=${token}&fields=id,name,picture`);
  const { id, picture, name } = await response.json();
  console.log("try facebook get", id);
  if (token) {
      dispatch({ type: TOKEN_RECEIVED, payload: token});
      dispatch({ type: SAVE_USER, payload: {id:id});
      getUserInfo(token, dispatch);
      console.log('got a token');
  }
}



async function getUserInfo(token, dispatch) {

  let response = await fetch(`https://graph.facebook.com/me?access_token=${token}&fields=id,name,picture`);
  const { id, picture, name } = await response.json();
  //console.log('picture' + JSON.stringify(await response.json().picture))
  console.log('the response is id: ' + id + " name " + name);
  return dispatch({ type: SAVE_USER, payload: {id: id} })
}

和save_user_reducer:

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

const INITIAL_STATE ={
    id: '',
    name: ''
  }; //empty initial state

export default (state=INITIAL_STATE, action) =>{
  console.log("save reducer called" + action.payload);
  switch(action.type){
    case SAVE_USER:
        return { ...state, [action.payload.prop]: action.payload.value};
    default:
        return state;
  }
};

1 个答案:

答案 0 :(得分:0)

我重新排序了如何将分派传递到getInfo(dispatch,token)的第二个函数,并且它起作用了!