Redux:将动作创建者导入另一个导致typeError'x不是函数'

问题描述 投票:0回答:1

我正面临一个类型错误,它真的让我困惑,让我有点生气:

我有一个汽车动作创作者:

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

有趣的是:如果我从'./buyer'中删除“import {notifyBuyer};”来自car.js一切都很好。但是一旦在文件中设置了导入,Whatever-Component的props就不再保存notifyBuyer()函数,并抛出typeError。

reactjs redux action
1个回答
0
投票

我认为在connect函数中使用的导出看起来是错误的。使用下面的示例代码并检查它。

export default connect(
  state => ({}),
  dispatch => ({notifyBuyer: dispatch(actions.notifyBuyer)})
)(StyledComponent); 
© www.soinside.com 2019 - 2024. All rights reserved.