react-native-vector-icons: 'XXX' cannot be used as a JSX component. Its instance type 'Icon' is not a valid JSX element.
- Review the documentation: https://github.com/oblador/react-native-vector-icons
- Search for existing issues (including closed issues): https://github.com/oblador/react-native-vector-icons/issues
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
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:
I had to place “paths” inside “compilerOptions” like this, but otherwise this worked for me also:
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.
Thanks, It works @gaithoben
I past the below code in my tsconfig.json file. it work for me.
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:
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
Worked for me!
I don’t think the workaround can be treated as a fix.
For me, specifying
reactpath in “paths” will break metro to resolvereactmodule.However, by installing the
@types/react-domdid solve the issue.For some reason, the package was using the types from v19:
I fixed this forcing v18 using “resolutions” in
package.jsonand runningyarnagain:This breaks builds in a hybrid RN x Expo project
I had the same error :
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-iconsdepends on"@types/react-native" "*"soyarnbundled both 0.69 and 0.70 versions of@types/react-native. Fix for me was addingto
package.json.It is a bit annoying though.