question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Difference between React$ComponentType and React.ComponentType?

See original GitHub issue

I’m currently experiencing an issue where replacing React$ComponentType with React.ComponentType in react-intl’s injectIntl() libdef fixes a type checking error.

iff --git a/flow-typed/npm/react-intl_v2.x.x.js b/flow-typed/npm/react-intl_v2.x.x.js
index 07744d6..2a78d3e 100644
--- a/flow-typed/npm/react-intl_v2.x.x.js
+++ b/flow-typed/npm/react-intl_v2.x.x.js
@@ -126,14 +126,14 @@ declare module "react-intl" {
     messageDescriptors: T
   ): T;
   declare function injectIntl<Props: {}>(
-    WrappedComponent: React$ComponentType<
+    WrappedComponent: React.ComponentType<
       { intl: $npm$ReactIntl$IntlShape } & Props
     >,
     options?: {
       intlPropName?: string,
       withRef?: boolean
     }
-  ): React$ComponentType<Props>;
+  ): React.ComponentType<Props>;
   declare function formatMessage(
     messageDescriptor: $npm$ReactIntl$MessageDescriptor,
     values?: Object

Although the above modification obviously does the job, and something I was about to open a PR for, I’d really appreciate understanding why that works rather than something I found out of luck by reading the React utility types 🤔

flow check error for reference
Error: src/components/more/MorePage.js:84
                 v----------------
 84:             <EnterDepositCode
 85:               onSubmit={submitDepositCode}
 86:               loading={submitDepositCodeLoading}
...:
 89:             />
                 -^ React element `EnterDepositCode`
 43: export class EnterDepositCode extends React.Component<Props, State> {
                                                           ^^^^^ property `intl`. Property not found in. See: src/components/more/EnterDepositCode.js:43
                 v----------------
 84:             <EnterDepositCode
 85:               onSubmit={submitDepositCode}
 86:               loading={submitDepositCodeLoading}
...:
 89:             />
                 -^ props of React element `EnterDepositCode`

Any hints?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
kalleycommented, Oct 4, 2017

To use React.ComponentType you have to use the star import (import * as React from 'react'). If you haven't imported react that way, those types are not available off of React. With the $` separator, you have access to the globally defined types.

1reaction
phillipjcommented, Oct 5, 2017

Aha, thanks a lot for explaining the differences!

With your comments, I assume that flow-typed libdefs are required to use React$ComponentType, but application code could use React.ComponentType as long as the entire React namespace has been required first.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Type Reference - JavaScript. Flow
A reference for all the utility Flow types exported by the React module. ... typeof Component . typeof Component is the component type...
Read more >
React component type in TypeScript - Stack Overflow
The correct type for a functional component is React.FunctionComponent or React.FC which is a shortcut alias for it
Read more >
React Top-Level API
The difference between them is that React. ... string (such as 'div' or 'span' ), a React component type (a class or a...
Read more >
Documentation - JSX - TypeScript
createElement("div") ), whereas a component you've created is not ( React. createElement(MyComponent) ). The types of the attributes being passed in the JSX ......
Read more >
Types of React Components. Functional, Class, Pure, and…
React components are independent and reusable code. They are the building blocks of any React application. Components serve the same purpose as JavaScript ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found