javascript - this.props.getEvents 不是一个函数

标签 javascript reactjs redux react-redux

我在 eventActions.js 中导出了 getEvents 定义。我不明白为什么我会收到此错误。我导入了该函数并且文件路径是正确的。

组件

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
      })
    );
};

最佳答案

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

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

关于javascript - this.props.getEvents 不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51685867/

相关文章:

javascript - 如何使用 JavaScript 更新进度条的值?

javascript - 在 react 中对字符串应用跨度会返回 [object Object] 而不是给我跨度文本

javascript - 如何使用新的 js async/await 语法使用 redux-thunk 中间件异步操作

javascript - 在 CRM 2011 中为从功能区打开的表单调试 javascript 时,如何创建脚本 block ?

javascript - 如何读取 JSON 文件并将键/值对加载到字典中

javascript - Reactjs es6,.map 函数不触发 onclick

javascript - 如何修复 React 中的 'eslint(no-unused-vars)' 错误

javascript - react | TypeError : this. state.cars.map 不是一个函数

javascript - Redux Action 创建者获得 "TypeError: dispatch(...) is not a function"(附代码)?

javascript - 组合组合 reducer