简体   繁体   English

'dispatch'未捕获的TypeError React-Redux

[英]'dispatch' Uncaught TypeError React-Redux

A quick background, I was working on a React-Redux app using an older version of this boiler plate project. 简要介绍一下,我正在使用样板项目的较旧版本开发React-Redux应用程序。

However, it had some issues going forward so I ported my code to the latest version of the same boiler plate project (did it yesterday). 但是,它存在一些问题,因此我将代码移植到同一样板项目的最新版本中(昨天完成)。

So the issue I had before got resolved. 因此,我之前遇到的问题已得到解决。 The UI is loading and everything looks great, but when Button.onTouchTap() gets fired, it's throwing an error like below: UI正在加载,一切看起来都很不错,但是当Button.onTouchTap()被触发时,它会引发如下错误:

Uncaught TypeError this.props.dispatch is not a function

This was working fine before without any problems, not sure what's breaking it. 之前没有任何问题,此方法工作正常,不确定是什么原因造成的。 :( :(

In my component, I use decorators to connect to the store, not mapStateToProps method (which is commonly the root of this problem afaik). 在我的组件中,我使用装饰器连接到商店,而不是mapStateToProps方法(通常是此问题afaik的根源)。

@connect((store) => {
  return {
    param1 : store.myReducer.param1,
    param2 : store.myReducer.param2,
    param3 : store.myReducer.param3,
    param4 : store.myReducer.param4,
  }
})

The error is getting thrown at the line below: 错误被抛出以下行:

this.props.dispatch(myAction(this.state.param1, this.state.param2))

I'm totally confused and have been caught on this for a long time. 我很困惑,已经被这个问题困扰了很长时间了。 Any help is appreciated, thanks! 任何帮助表示赞赏,谢谢! <3 <3

It is in general considered a bad practice to directly give access to the dispatch function in component. 通常,直接授予对组件中分派功能的访问权限的做法通常被认为是不良做法。 You should instead use the second argument of @connect decorator to map the actions you want to dispatch to the props of your component as simple callbacks. 相反,您应该使用@connect装饰器的第二个参数将要调度的操作作为简单的回调映射到组件的props。

This pattern makes your component dumb. 这种模式会使您的组件变得笨拙。 It doesn't know that the callback is coming from redux or from its parent or from whatever other sources you can use. 它不知道该回调来自redux或其父对象或其他任何您可以使用的来源。 It reduce adherence of your component and makes it easier to test and maintain. 它减少了组件的依从性,并使测试和维护更加容易。

I have never used connect as a decorator but I suppose that you can use it this way 我从未将connect用作装饰器,但我想您可以通过这种方式使用它

import { connect } from 'react-redux'
import { myAction } from '/path/to/action/file'

@connect(
  state => ({ param: state.myReducer.myParam }),
  { myAction }
)

const MyComponent = props => {
  const { param, myAction } = props

  return (
    <div>
      <p>{ param }</p>
      <button onClick={ myAction }>Click Me</button>
    </div>
  )
}

export default MyComponent

Bad practice solution 不良做法解决方案

If you really want your code to juste work as it is juste connect you component and give it the dispatch function in props. 如果您真的希望您的代码能够正常工作,那么请连接您的组件,并在props中为其分配函数。

import { connect } from 'react-redux'
import { myAction } from '/path/to/action/file'

@connect(
  state => ({ param: state.myReducer.myParam }),
  dispatch => ({ dispatch }),
)

const MyComponent = props => {
  const { param, dispatch } = props

  return (
    <div>
      <p>{ param }</p>
      <button onClick={ () => dispatch(myAction()) }>Click Me</button>
    </div>
  )
}

export default MyComponent

But again, this should not be done in real world application and should be refactored as showed in the beginning of this answer. 但是同样,这不应在现实世界的应用程序中完成,而应如本答案开头所示进行重构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM