简体   繁体   English

无法调用`bindActionCreators`,因为`Action` [1]与`ActionCreator`不兼容

[英]Cannot call `bindActionCreators` because `Action` [1] is incompatible with `ActionCreator`

I have a React-Redux app and I'm using Flow for the types (I've followed the official guide here ). 我有一个React-Redux应用程序,并且我在使用Flow作为类型(我在这里遵循了官方指南)。 Everything works just fine but I keep getting this annoying error message that I can't figure out how to solve: 一切正常,但我不断收到此烦人的错误消息,无法解决:

Cannot call bindActionCreators because Action 1 is incompatible with ActionCreator 不能打电话bindActionCreators因为Action 1格格不入ActionCreator

The error is in mapDispatchToProps in the main component, App: 错误mapDispatchToProps在主要组件App中的mapDispatchToProps中:

App.js App.js

import { bindActionCreators } from 'redux'

import * as actions from '../actions/index'
import type { Action, Dispatch } from '../actions/index'
// ... more imports

const mapDispatchToProps = (dispatch: Dispatch) => {
  return {
    boundActionCreators: bindActionCreators(actions, dispatch)
  }
}

// ... mapStateToProps, class definition and so on

Then I have all my actions defined in actions/index like so: 然后,我将所有动作定义在动作/索引中,如下所示:

actions/index.js 动作/ index.js

export type ToggleLoading = {
    type: "TOGGLE_LOADING",
    loading: boolean
}

function toggleLoading (loading: boolean): ToggleLoading {
  return { type: 'TOGGLE_LOADING', loading }
}

export type Action = ToggleLoading | ...

export type Dispatch = (action: Action | ThunkAction | PromiseAction | Array<Action>) => any
export type PromiseAction = Promise<Action>
export type ThunkAction = (dispatch: Dispatch, getState: GetState) => any

export{
  toggleLoading
}

Have anyone any idea what is the cause of this error? 有谁知道这个错误的原因是什么? Maybe just Flow? 也许只是流?

Simply return bindActionCreator in your mapDispatchToProps: 只需在mapDispatchToProps中返回bindActionCreator:

const mapDispatchToProps = (dispatch: Dispatch) => bindActionCreators(actions, dispatch)

I've read and use this blog post to setup Flow/React/Redux: 我已阅读并使用此博客文章来设置Flow / React / Redux:

type checking react redux 类型检查反应redux

I got this error too today. 我今天也收到这个错误。 It's a strange error because even though I had the same pattern on other files, I'm getting this error on only one of the files. 这是一个奇怪的错误,因为即使我在其他文件上具有相同的模式,我也只会在其中一个文件上收到此错误。 What I did was switched from node@6 to node@8 and it fixed the error for me. 我所做的是从node@6切换到node@8 ,它为我修复了错误。 However, I still find it strange why would I get the error only on a specific file and not the others which pretty much have the same code. 但是,我仍然感到奇怪,为什么我只在特定文件上而不是在其他大多数具有相同代码的文件上才得到错误。

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

相关问题 调用actionCreator函数无法通过bindActionCreators工作 - Calling actionCreator function is not working through bindActionCreators 从另一个 ActionCreator 调用一个 ActionCreator - Call an ActionCreator from another ActionCreator 来自 actionCreator 的调度动作 - Dispatch action from actionCreator Redux 操作仅适用于 bindActionCreators - Redux action working only with bindActionCreators redux bindActionCreators分配了任何动作 - redux bindActionCreators any action is dispached 初始化前无法访问“actionCreator” - Cannot access 'actionCreator' before initialization 期望bindActionCreators函数actionCreator用于键&#39;ACCOUNT_LOGIN&#39;,而是接收类型&#39;string&#39; - bindActionCreators expected a function actionCreator for key 'ACCOUNT_LOGIN', instead received type 'string' 在 Flux 中,让通用组件调用 ActionCreator 是否符合犹太教规? - In Flux, is it kosher to have generic components call the ActionCreator? 流输入错误:无法使用绑定到操作的对象文字调用调度,因为: - Flow typing error : Cannot call dispatch with object literal bound to action because: React JS Jest 单元测试 SpyOn ComponentDidMount 操作调用,获取无法监视.. 属性,因为它不是 function - React JS Jest Unit Testing SpyOn ComponentDidMount action call, Getting Cannot spy on .. property because it is not a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM