javascript - 无法将数据从操作传递到 reducer

标签 javascript reactjs redux

从服务器获取响应并尝试调度

我的行动:

import axios from 'axios'
import { INCOME_LIST } from '../actionTypes'

    function receiveData(json) {
        return{

            type: INCOME_LIST,
            data: json
        }
    };


    export function IncomeList () {

        return dispatch => {

            return (
                console.log(response.data);
                axios.post('http://api.hylaa.net:8084/course/income/outline',{}, {
          headers: { 'X-Authenticated-Userid': '15000500000@1' }
         }).then(function (response) {

                    dispatch(receiveData(response.data.body));

                })

                )
        }
    }

我的 reducer :

import { INCOME_LIST } from '../actionTypes'

import Immutable from 'immutable'

const initialUserState = {
  list: [
            {
                "last_month": 501,
                "current_month": 1021,
                "total": 1521
             }

         ]
}

    const listReducer = function(state = initialUserState, action) {
       console.log('actiondata in reducer:' + action.data);

      switch(action.type) {

      case 'INCOME_LIST': 
             return Object.assign({}, state, { list: action.data });

      default: 

      return state;
      }


}

export default listReducer

console.log('reducer 中的actiondata:' + action.data); - 何时未定义。

尝试在 Action 中使用 console.log 进行记录,console.log 不起作用。如何将数据从Action传递到reducer?我认为我需要在组件中初始化 Action 吗?

尝试在组件中执行此操作

import { IncomeList } from '../../actions/income'
    componentDidMount() {

      this.props.IncomeList()

        } - says ImcomeList is not a func

最佳答案

您需要从组件调用您的操作。为此,您需要首先将其绑定(bind)到 props。为此,请使用 mapDispachToProps 组件

import {IncomeList} form './path/to/MyAction';
import { bindActionCreators } from 'redux';

class App extends React.Component {
    constructor(props) {
       super(props);
    }
    componentDidMount() {
      this.props.IncomeList
    }
    render() {
         ......
    }
}

function mapDispatchToProps(dispatch) {
    return {
       IncomeList: bindActionCreators({IncomeList}, dispatch);
    }
}

关于javascript - 无法将数据从操作传递到 reducer ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41801342/

相关文章:

javascript - 如何跟踪外部 JSON 文件的更改

javascript - JQuery 中 slider 标题的问题

javascript - 从输入文本中获取数字,然后在单击按钮时递增 ReactJS

javascript - createStore with combineReducers 和 applyMiddleware 没有第二个参数

javascript - 在 Angular 5 中从 .subscribe() 获取数据后如何调用函数

reactjs - 根据 Action react 更新网址

reactjs - 使用 prevState 和 useState 钩子(Hook)更新对象内数组内的对象

javascript - 缓慢时如何加快 native react 中的联系人处理速度?

javascript - 如何在redux操作中获取userid?

javascript - .get() 不过滤特定的 css 选择器