【问题标题】:How to get combineReducers working with flowtype?如何让 combineReducers 与 flowtype 一起工作?
【发布时间】:2019-02-24 00:25:32
【问题描述】:

我想使用 redux 中的 combineReducers 函数。 但是我收到以下错误消息:

Missing type annotation for `A`. `A` is a type parameter declared in function type [1] and was implicitly instantiated
at call of `combineReducers` [2].

   src/reducer/index.js:12:16
   12| export default combineReducers({ message })
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [2]

References:
   flow-typed/npm/redux_v4.x.x.js:56:42
   56|   declare export function combineReducers<O: Object, A>(reducers: O): CombinedReducer<$ObjMap<O, <S>(r: Reducer<S, any>) => S>, A>;
                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [1]

我的 reducer 只是一个获取状态和动作并返回的函数 一个新的状态。

然后我只是在reducer上调用combineReducers,如错误消息所示。

有人知道解决此问题的简单方法吗?

【问题讨论】:

    标签: redux flowtype


    【解决方案1】:

    我找到了解决办法。

    您必须以某种方式输入reducer 的结果。 你实际上可以做两件事,第一件事是:

    export default combineReducers<*, *>({ message })
    

    对我来说,这感觉就像一个 hack。 更好的解决方案是:

    type State = {
        message: MessageState
    }
    const reducers: Reducer<State, Action> = combineReducers({ message })
    export default reducers
    

    但是,这需要您跟踪状态和操作的类型, 无论如何你都应该这样做。

    【讨论】:

    猜你喜欢
    • 2023-02-02
    • 2017-06-25
    • 2020-12-13
    • 2012-02-02
    • 2011-02-22
    • 2013-03-24
    • 2016-07-22
    • 2011-08-17
    • 2021-12-18
    相关资源
    最近更新 更多