当我想调度一个 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/