redux - TypeError : Object(. ..) 不是函数(redux)

标签 redux

当我想调度一个 Action 时,我遇到了 TypeError(TypeError: Object(...) is not a function)。我没有使用任何中间件,也不知道我能做些什么来解决它。我昨天已经遇到了这个错误,但不知何故设法解决了它(我不知道我是怎么做到的)

这是 App.js:

import React from "react";
import { store } from "../store";
import { withdrawMoney} from "../actions";

const App = () => {
return (
  <div className="app">
    <div className="card">
      <div className="card-header">
        Welcome to your bank account
      </div>
      <div className="card-body">
        <h1>Hello, {store.getState().name}!</h1>
        <ul className="list-group">
          <li className="list-group-item">
            <h4>Your total amount:</h4>
            {store.getState().balance}
          </li>
        </ul>
        <button className="btn btn-primary card-link" data-amount="5000" onClick={dispatchBtnAction}>Withdraw $5,000</button>
        <button className="btn btn-primary card-link" data-amount="10000" onClick={dispatchBtnAction}>Witdhraw $10,000</button>
      </div>
    </div>
  </div>
);
}

function dispatchBtnAction(e) {
  store.dispatch(withdrawMoney(e.target.dataset.amount));
}

export default App;

这是 Action 创建器:
function withdrawMoney(amount) {
  return {
    type: "ADD_TODO",
    amount
  }
}

如果您需要这里是 reducer :
export default (state, action) => {
  console.log(action);
  return state
}

正如你所看到的,我对 redux 非常陌生,但我想知道我在调度 action 时总是犯什么错误。谢谢

最佳答案

我认为问题在于您没有导出withdrawMoney 函数,因此您无法在尝试导入的组件中调用它。

尝试:

export function withdrawMoney(amount) {
  return {
    type: "ADD_TODO",
    amount
  }
}

关于redux - TypeError : Object(. ..) 不是函数(redux),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50728865/

相关文章:

reactjs - Redux:如何将 ID 传递给 mapStateToProps 函数以从状态中获取具有该 ID 的实体

typescript - 如何使用 RootState 修复切片的循环依赖关系?

javascript - 使用 moxios 测试 redux 异步方法

javascript - Reduxjs 或简单地声明

javascript - 通过 React Router 传递在 app.jsx 级别创建的 props

javascript - 如何将动态创建的 props 传递给组件?

javascript - 在react-redux组件中获取初始数据时显示加载器

javascript - 如何预加载网络 worker Assets

javascript - 使用 react-redux 绑定(bind)事件处理程序 prop

javascript - React/Redux - 假设我们有两个单独的待办事项列表,已完成和未完成