2017-03-21 72 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();