6

I'm adding typescript to my react-native app,

in App.tsx

I'm getting this error on Cannot find name React$Node on

const App: () => React$Node = () => {
 ...
}
ImFarhad
  • 2,669
  • 2
  • 18
  • 30

1 Answers1

7

I'm unsure of the syntax you're using, and not sure where React$Node comes from. From my experience, the correct typing and syntax should be React.ReactNode or React.ReactElement. For example:

import React from 'react'

const App = (): React.ReactElement => { 

  return (
    // component code
  )
}

As for the difference between ReactNode and ReactElement (because I wasn't entirely sure), I found ford04's answer useful - quoted here:

ReactElement and JSX.Element are the result of invoking React.createElement directly or via JSX transpilation. It is an object with type, props and key. [JSX.Element]

ReactNode is used as return type for render() in class components. It also is the default type for children attribute with [PropsWithChildren]

Tom Oakley
  • 6,065
  • 11
  • 44
  • 73
  • 3
    I've just looked up `React$Node` - it's a [Flow](https://flow.org) type. So the corresponding type for TypeScript will be `ReactNode` (or `React.ReactNode` if you don't import it). Make sure you install [`@types/react`](https://www.npmjs.com/package/@types/react) and [`@types/react-native`](https://www.npmjs.com/package/@types/react-native). – Tom Oakley Apr 24 '20 at 15:11