首页 > 解决方案 > this.props.getEvents 不是函数

问题描述

我在导出的eventActions.js. 我无法弄清楚为什么会收到此错误。我导入了函数,文件路径是正确的。

零件

import { getEvents } from "../../actions/eventActions";

...

componentDidMount() {
  this.props.getCurrentProfile();
  this.props.getEvents();
}

eventActions.js

// Get Events
export const getEvents = () => dispatch => {
  dispatch(setEventsLoading());
  axios
    .get("/api/events")
    .then(res =>
      dispatch({
        type: GET_EVENTS,
        payload: res.data
      })
    )
    .catch(err =>
      dispatch({
        type: GET_EVENTS,
        payload: null
      })
    );
};

标签: javascriptreactjsreduxreact-redux

解决方案


Props是由父组件传递给其子组件的东西。在这里,您是从文件中导入它,而不是从父组件传递。

您可以简单地使用 -getEvents()而不是this.props.getEvents().


推荐阅读