Experiencing compile error with a higher order component's mapState function that returns an object within an object

前端 未结 1 452
执笔经年
执笔经年 2020-12-07 06:00

30/11/2019 Updated the code sandbox with the solution here.

Original Question asked on 28/11/2019 I am trying to learn Higher Order

相关标签:
1条回答
  • 2020-12-07 06:26

    When you're injecting Props by using mapStateToProps and dispatchProps in a connect you have to subtract these props from the BaseProps generic type by using the Diff helper type. Moreover, if you want to use these injected Props in the Hoc component you have to pass them as the Component type argument. e.g. React.Component<InjectedProps>.

    This is a reason for the errors.

    So in your example props are:

    type InjectedProps = TStateProps & TDispatchProps;
    
    type HocProps = Diff<BaseProps, InjectedProps>;
    
    class Hoc extends React.Component<InjectedProps> { ... }
    
    const ConnectedHoc = connect<
        TStateProps,
        TDispatchProps,
        HocProps,
        RootState
      >(
        mapStateToProps,
        dispatchProps
      )(Hoc);
    

    Also, please check the same discussion in the react-redux-typescript-guide

    0 讨论(0)
提交回复
热议问题