代码之家  ›  专栏  ›  技术社区  ›  Slava Knyazev

使用Flowtype覆盖React Redux Connect()

  •  1
  • Slava Knyazev  · 技术社区  · 6 年前

    我有以下代码:

    const mapStateToFilterProps = (state:DataExplorerState, props) => ({ loading: state.loading, filters: state.filters });
    
    const actionCreators: ActionCreators<string, Action> = { updateLoadState, updateFilters, updateChartData};
    
    const mapDispatchToProps = (dispatch: Dispatch<Action>) => bindActionCreators(actionCreators, dispatch)
    
    // TODO: Fix typing issue
    export const FilterBoxConnect = connect(
        mapStateToFilterProps, 
        mapDispatchToProps
    )(FilterBox)
    

    “TODO”下面的代码表示它被发现了。我该怎么掩饰?

    1 回复  |  直到 6 年前
        1
  •  1
  •   wgao19    6 年前

    我想是因为 connect 在0.85之前不需要显式注释。0.85之后,流量将 ask for required annotations . 基本上,如果我们在导出时不显式地注释连接的组件,Flow将报告“隐式实例化”错误:

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

    另外,我们现在可以显式地注释 连接 通过提供类型参数。

    import * as React from 'react'
    
    type OwnProps = {|
      passthrough: string,
      forMapStateToProps: string
    |}
    
    type Props = {|
      ...OwnProps,
      fromMapStateToProps: string,
      dispatch1: () => void
    |}
    
    const MyComponent = (props: Props) => (
      <div onClick={props.dispatch1}>
        {props.passthrough}
        {props.fromMapStateToProps}
      </div>
    )
    
    export default connect<Props, OwnProps, _, _, _, _>(
      mapStateToProps,
      mapDispatchToProps
    )(MyComponent)
    

    我有更详细的指南 here .

        2
  •  0
  •   Slava Knyazev    6 年前

    这个问题可能已经解决了。我不知道,我没有测试,但维修人员声称是这样的: Exhibit A , Exhibit B