2017-03-21 42 views
0

我真的不知道为什么这个操作不起作用。返回调度功能(仅适用于“工作”被记录)react/redux请勿派遣

export function authAdmin({nickname, password}){ 
     console.log('working'); 
    return function(dispatch){ 
     console.log('still working'); 
    axios.post(`${URL}/admin`, {nickname, password}) 
      .then(response => { 
      console.log(response); 
      localStorage.setItem('token', response.data.token); 
      dispatch({type: AUTH_ADMIN}); 
      browserHistory.push('/'); 
      }) 
      .catch((err) => { 
      console.log(err); 
      }); 
     } 
    } 

但几乎相同的动作正在使用没有问题之前,采取行动停止。我错过了明显的东西?

 export function getPosts(page){ 
    return function(dispatch){ 
     dispatch({ type: IS_FETCHING }); 
     axios.get(`${URL}/home?page=${page}`) 
      .then(response => { 
      dispatch ({ 
      type: FETCH_POSTS, 
      payload: response.data   
      }) 
      }) 
      .catch((error) => { 
      dispatch({ type: ERROR_FETCHING }); 
      }); 
    } 
    } 
+0

你是如何调用每个动作创造者?我的猜测是'getPosts()'被正确调度,而'authAdmin()'实际上并没有被调度(只是单独执行)。 – markerikson

+0

是的,这是:)谢谢:) – Kar

回答

0

为后人:

这时候你本身调用形实转换功能,这意味着内部函数不会被传递到dispatch()通常发生:

import {someThunkActionCreator} from "./actions"; 

// Wrong - just returns the inner function, but never runs it 
someThunkActionCreator(); 

// Right - passes the inner function to dispatch, which runs it 
dispatch(someThunkActionCreator()) 

// Right - creates a bound-up version which auto-dispatches 
const boundThunk = bindActionCreators(someThunkActionCreator, dispatch); 
boundThunk();