[英]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
becauseAction
1 is incompatible withActionCreator
不能打电话
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:
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.