2016-12-01 40 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);