2016-12-01 42 views
0

我遇到問題,我的操作不會進入我的返回語句。我已經從我已經開發過的項目或多或少地複製了代碼,並知道它的工作原理。如果有任何redux主人,那裏的幫助將不勝感激。React-Redux操作不會發送到還原器

import * as types from '../Constants/Action_Types' 
import axios from 'axios' 

export function getSingleReport(){ 
    console.log("in the action") 
    return function (dispatch) { 
     console.log("in the action return function") 
     axios.get('/api/report-single/57b3aba0aa676af0a3db6250').then((result) => dispatch({ 
      type: types.GET_SINGLE_REPORT, 
      reportTitle: result.data.title, 
      reportAuthor: result.data.author 
     })) 
    } 

} 

我會打第一個conole.log聲明,但不是第二個。爲什麼行動決定停止在返回功能(dispatch){line?

按照要求,這裏還有減速機。

import { GET_SINGLE_REPORT } from '../Constants/Action_Types' 

const initialState = { 
    reportTitle: "Title of Report", 
    reportAuthor: "Author of Report" 
} 

export default function populateReport(state = initialState, action) { 
    console.log("in the reducer") 
    switch (action.type) { 
     case GET_SINGLE_REPORT: 
     console.log("in the get GET_SINGLE_REPORT reducer") 
     return { 
      ...state, 
      reportTitle: action.reportTitle, 
      reportAuthor: action.reportAuthor 
     } 

     default: 
      return state 
    } 
} 
+0

沒有上下文,它是不可能的幫助。也許你沒有調用返回的函數? –

+0

您是否配置了'redux-thunk'中間件?聽起來像這個項目沒有舊項目的適當中間件。 –

+0

我確實配置了thunk中間件。第二個控制檯應在異步調用之前打印 – Peter3

回答

0

你是如何導入和調用你的getSingleReport函數?在處理返回函數的導入函數時,很容易出錯,並最終錯誤地調用它們。第二個console.log永遠不會被執行的事實意味着我永遠不會執行返回的函數。無論文件使用您發佈需要看起來像代碼...

import {getSingleReport} from './yourScriptLocation' 

const dispatchSingleReport = getSingleReport(); 

const dispatch = // wherever your dispatch comes from, doesn't really matter 

dispatchSingleReport(dispatch); 

如果你只需要調用的進口功能,如跌破的話,這可以解釋你所看到的行爲,因爲返回的功能將被丟棄。

import {getSingleReport} from './yourScriptLocation' 

const dispatch = // wherever your dispatch comes from, doesn't really matter 

getSingleReport(dispatch);