reactjs - Redux:将动作创建者导入另一个导致 typeError 'x is not a function'
问题描述
我正面临一个 typeError ,这真的让我很困惑,让我有点发疯:
我有一个汽车动作创建者:
import * as dispatchTypes from '../helper/dispatchTypes';
import { notifyBuyer } from './buyer';
export const addCar = () => async dispatch => {
const response = await fetch(someUrl, someBody)
if (response.ok) {
const car = await response.json();
dispatch({type: dispatchTypes.ADD_CAR_RESPONSE, car});
return notifyBuyer()(dispatch);
};
}
我在buyer.js 中也有一个notifyBuyer() 的动作创建者:
...
export const notifyBuyer = () => async dispatch => {
...
最后,我也在 React 组件中调用 notifyBuyer():
import * as actions from '../../actions/buyer';
class WhateverComponent extends React.Component {
constructor(props) {
super(props);
}
doSomething = event => {
if (!event.disabled) {
this.props.notifyBuyer(event.toString());
}
};
render() {...}
}
export const StyledComponent = withStyles(styles)(WhateverComponent);
export default connect(
state => ({}),
{notifyBuyer: actions.notifyBuyer}
)(StyledComponent);
如果我运行应用程序并使 doSomething 函数运行,我会收到以下错误:
TypeError: _this.props.notifyBuyer is not a function
有趣的是:如果我删除“import { notifyBuyer } from './buyer';” 从 car.js 一切都很好。但是一旦在文件中设置了导入,Whatever-Component 的 props 就不再持有 notifyBuyer() 函数,并且抛出 typeError 。
解决方案
我认为连接函数中使用的导出看起来是错误的。使用下面的示例代码并检查一下。
export default connect(
state => ({}),
dispatch => ({notifyBuyer: dispatch(actions.notifyBuyer)})
)(StyledComponent);
推荐阅读
- tsql - 如何在 SQL 存储过程中编写条件?
- azure - 通过管理组强制所有 Azure 访问
- python - 读取 csv 时 Pandas dtype 选项错误
- azure - 如何解决 Azure 应用设置密钥长度限制
- c# - 机器人模拟器 - 无法调试
- javascript - while循环内的多个选择标签下拉列表,但仅从最后一个中检索数据
- javascript - 如何使用 Vue 和 json 获取特定数组
- c++ - 我应该为 QWidget 的派生定义一个析构函数吗
- php - 变异中的 Laravel LightHouse GraphQL DateTime 输入始终为空
- python - 未解决的参考,我不知道为什么