2016-12-01 14 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ステートメントをヒットしますが、2番目のステートメントはヒットしません。なぜアクションはリターン関数(ディスパッチ){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

私はサンクミドルウェアを設定しました。非同期呼び出しが行われる前に、第2のコンソールが印刷されます。 – Peter3

答えて

0

getSingleReport関数をどのようにインポートして呼び出していますか?関数を返すインポートされた関数を扱うときに間違いを起こしやすくなり、誤って呼び出すことになります。 2番目の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); 
関連する問題