react-native-vector-icons: 'XXX' cannot be used as a JSX component. Its instance type 'Icon' is not a valid JSX element.

Environment

windows11

Description

When I upgraded to react18,my icon component reported an error.Such as:

 'MaterialCommunityIcons' cannot be used as a JSX component.
  Its instance type 'Icon' is not a valid JSX element.

this is my code:

import React ,{ useEffect, useState } from "react";
import { Platform, View } from "react-native";
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons'

const App = () => {
  const [device, setDevice] = useState<string>('')
  useEffect(() => {
    if (Platform.OS === 'ios') {
      setDevice('ios')
    } else {
      setDevice('android')
    }
  }, [])

 return (
    <View>
       <MaterialCommunityIcons name="calendar" onPress={showDatepicker} size={20} />
    </View>
 )
}

export default App

this is my pakage.json

"dependencies":{
     "expo": "^45.0.6",
     "react": "^18.2.0",
     "react-dom": "^18.2.0",
     "react-native": "^0.69.1",
     "react-native-vector-icons": "^9.2.0",
 },
"devDependencies": {
    "@babel/core": "^7.12.9",
    "@types/react": "^18.0.14",
    "@types/react-dom": "^18.0.5",
    "@types/react-native": "^0.69.2",
    "typescript": "~4.3.5"
  },
"resolutions": {
    "@types/react": "~18.0.14"
  }

How to solve this problem?

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 10
  • Comments: 27

Most upvoted comments

The Issue is with Latest Version of @types/react:18.x.x This may have been introduced to your project via some other packages’ peer dependency.

If you have a .tsconfig file, Add following to compilerOptions:

"paths": {
   "react": [ "./node_modules/@types/react" ]
 }

The Issue is with Latest Version of @types/react:18.x.x This may have been introduced to your project via some other packages’ peer dependency.

If you have a .tsconfig file, Add following to compilerOptions:

"paths": {
   "react": [ "./node_modules/@types/react" ]
 }

I had to place “paths” inside “compilerOptions” like this, but otherwise this worked for me also:

  "compilerOptions": {
    "paths": {
      "react": ["./node_modules/@types/react"]
    }
  },

The Issue is with Latest Version of @types/react:18.x.x This may have been introduced to your project via some other packages’ peer dependency. If you have a .tsconfig file, Add following to compilerOptions:

"paths": {
   "react": [ "./node_modules/@types/react" ]
 }

I had to place “paths” inside “compilerOptions” like this, but otherwise this worked for me also:

  "compilerOptions": {
    "paths": {
      "react": ["./node_modules/@types/react"]
    }
  },

Works on mine. Thanks

None of the fixes here work for me - I’m using react: 18 react-native: 0.73.4

both packages and their @types packages are only present once (as reported by yarn why) and so there doesn’t seem to be a version conflict.

The compilerOptions workaround did remove the warning in the IDE but created a compile error unable to resolve react.

The Issue is with Latest Version of @types/react:18.x.x This may have been introduced to your project via some other packages’ peer dependency.

If you have a .tsconfig file, Add following to compilerOptions:

"paths": {
   "react": [ "./node_modules/@types/react" ]
 }

Thanks, It works @gaithoben

The Issue is with Latest Version of @types/react:18.x.x This may have been introduced to your project via some other packages’ peer dependency. If you have a .tsconfig file, Add following to compilerOptions:

"paths": {
   "react": [ "./node_modules/@types/react" ]
 }

I had to place “paths” inside “compilerOptions” like this, but otherwise this worked for me also:

  "compilerOptions": {
    "paths": {
      "react": ["./node_modules/@types/react"]
    }
  },

Works on mine. Thanks

I past the below code in my tsconfig.json file. it work for me.

  "compilerOptions": {
    "paths": {
      "react": ["./node_modules/@types/react"]
    }
  },

I had the same issue and I tried all those as mentioned above. But what works for me is to update the @types/node and @types/react to the latest version.

run command: yarn up @types/node @types/react

I’m using this version: “react”: “18.3.1”,
“react-native”: “0.77.0”,

For me, the solution was to update in the package.json:


  "resolutions": {
    "@types/react": "18.0.24",
    "@types/react-dom": "18.0.2"
  }

The Issue is with Latest Version of @types/react:18.x.x This may have been introduced to your project via some other packages’ peer dependency.

If you have a .tsconfig file, Add following to compilerOptions:

"paths": {
   "react": [ "./node_modules/@types/react" ]
 }

This solved the issue. Thanks for the solution.

I’m running into the same problem.

You can see this issue: https://github.com/oblador/react-native-vector-icons/issues/1405

The Issue is with Latest Version of @types/react:18.x.x This may have been introduced to your project via some other packages’ peer dependency.

If you have a .tsconfig file, Add following to compilerOptions:

"paths": {
   "react": [ "./node_modules/@types/react" ]
 }

Worked for me!

I don’t think the workaround can be treated as a fix.

For me, specifying react path in “paths” will break metro to resolve react module.

However, by installing the @types/react-dom did solve the issue.

For some reason, the package was using the types from v19:

I fixed this forcing v18 using “resolutions” in package.json and running yarn again:

"resolutions": {
    "@types/node": "^22",
    "@types/react": "^18.3.12"
  },

The Issue is with Latest Version of @types/react:18.x.x This may have been introduced to your project via some other packages’ peer dependency.

If you have a .tsconfig file, Add following to compilerOptions:

"paths": {
   "react": [ "./node_modules/@types/react" ]
 }

This breaks builds in a hybrid RN x Expo project

I had the same error :

error TS2786: 'Icon' cannot be used as a JSX component.
  Its type 'typeof Icon' is not a valid JSX element type.
    Type 'typeof Icon' is not assignable to type 'new (props: any, deprecatedLegacyContext?: any) => Component<any, any, any>'.
      Construct signature return types 'Icon' and 'Component<any, any, any>' are incompatible.
        The types returned by 'render()' are incompatible between these types.
          Type 'import("/home/grubshka/devel/apimoov/apps/beeobs-react-native/node_modules/@types/react-native-vector-icons/node_modules/@types/react/index").ReactNode' is not assignable to type 'React.ReactNode'.
            Type 'ReactElement<any, string | JSXElementConstructor<any>>' is not assignable to type 'ReactNode'.
              Property 'children' is missing in type 'ReactElement<any, string | JSXElementConstructor<any>>' but required in type 'ReactPortal'.

136           <Icon name="microphone" size={24} color="#000" />
               ~~~~

The Issue is with Latest Version of @types/react:18.x.x This may have been introduced to your project via some other packages’ peer dependency. If you have a .tsconfig file, Add following to compilerOptions:

"paths": {
   "react": [ "./node_modules/@types/react" ]
 }

This solved the issue for me too. Thanks for the solution.

In my case problem is my project is on RN 0.69 and @types/react-native-vector-icons depends on "@types/react-native" "*" so yarn bundled both 0.69 and 0.70 versions of @types/react-native. Fix for me was adding

resolutions": {
  "@types/react-native": "~0.69.1"
}

to package.json.

It is a bit annoying though.