加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

reactjs – 在没有传递任何道具的情况下实例化Component时,Flow

发布时间:2020-12-15 16:20:14 所属栏目:百科 来源:网络整理
导读:我有一个包含在react-redux“connect”中的组件.组件的所有道具都由mapStateToProps和MapDispatchToProps提供,因此没有“ownProps”传递给组件. 但是,我收到以下流错误: Cannot create Credits element because props [1] is incompatible with empty [2].
我有一个包含在react-redux“connect”中的组件.组件的所有道具都由mapStateToProps和MapDispatchToProps提供,因此没有“ownProps”传递给组件.

但是,我收到以下流错误:

Cannot create Credits element because props [1] is incompatible with empty [2].

     src/components/ChildrenView/index.js
     323│         />
     324│
     325│         {/* Credits */}
 [1] 326│         <Credits />
     327│
     328│         {/* fullscreen pictures rhat open with onClick on tge image */}
     329│         <PhotoViewer />

     flow-typed/npm/react-redux_v5.x.x.js
 [2] 110│     CP: $Diff<ElementConfig<Com>,RSP>,

1的道具是不是已经空了?

我正在使用flow-typed,因为你可以在错误中说明.

这是类定义和连接调用:

type Props = {|
  ...mapStateToPropsType,pricingModal: typeof pricingModal,offlineModal: typeof offlineModal,|}


class Credits extends React.Component<Props> { ... }


type mapStateToPropsType = {|
  balance: number,isVisiblePricing: boolean,isConnected: boolean,isVisibleOffline: boolean,|}

const mapStateToProps = ({ parents,pricing,appState }: TP.State): mapStateToPropsType => ({
  balance: parents.balance || 0,isVisiblePricing: pricing.modalPricing,isConnected: appState.isConnected,isVisibleOffline: appState.modalOffline,})

export default connect(mapStateToProps,{ pricingModal,offlineModal })(Credits)

如何删除此错误(不使用$FlowFixMe:/)?

编辑1

如果我在react-redux_v5.x.x.js的第110行和第114行的“连接”类型定义上进行了以下更改,则类型检查按预期工作.

之前:

declare export function connect<
    Com: ComponentType<*>,S: Object,SP: Object,RSP: Object,MDP: Object,CP: $Diff<ElementConfig<Com>,>(
    mapStateToProps: MapStateToProps<S,SP,mapDispatchToProps: MDP,): (component: Com) => ComponentType<$Diff<CP,MDP> & SP>

后:

declare export function connect<
    Com: ComponentType<*>,CP: $Diff<$Diff<ElementConfig<Com>,MDP>,/*  <-- here */
  >(
    mapStateToProps: MapStateToProps<S,): (component: Com) => ComponentType<CP & SP> /*  <-- here */

解决方法

我无法评论James Kraus的回答,因为我没有足够的声誉,但我可以确认更新到最新版本的flow(0.71)为我修复了它.我在30分钟前偶然发现了同样的问题.我没有必要更新流式类型的定义.接受詹姆斯的答案,如果它也适合你.

虽然,在我的情况下,我仍然必须使用$FlowFixMe,因为我使用反应本机并且更新到任何高于0.67.x的版本会破坏其他所有内容.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读