Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Svg' cannot be used as a JSX component #1741

Closed
caiorrsdeg opened this issue Apr 8, 2022 · 9 comments
Closed

'Svg' cannot be used as a JSX component #1741

caiorrsdeg opened this issue Apr 8, 2022 · 9 comments

Comments

@caiorrsdeg
Copy link

*First bug open here, sorry if not doing it right

Bug

When using an SVG converted with SVGR Typescript raises an error about '...' cannot be used as a JSX component, where ... is a component from react-native-svg
for example:

(alias) const Svg: React.ComponentClass<SvgProps, any>
import Svg
'Svg' cannot be used as a JSX component.
  Its instance type 'Component<SvgProps, any, any>' is not a valid JSX element.
    The types returned by 'render()' are incompatible between these types.
      Type 'import("/Users/caio/Projects/ufe-mobile/node_modules/@types/react/index").ReactNode' is not assignable to type 'React.ReactNode'.
        Type '{}' is not assignable to type 'ReactNode'.ts(2786)

tested on 12.1.0 (what we are using) and 12.3.0 (latest) with expo 44
The project uses a builder bob made package

"react": "17.0.2",
"react-native": "0.67.2",
"react-native-builder-bob": "^0.18.0",
"react-test-renderer": "17.0.2",
"typescript": "^4.5.5",

example project inside the package

"expo": "^44.0.0",
"react": "17.0.1",
"react-dom": "17.0.1",
"react-native": "0.64.3",

Unexpected behavior

The example expo app is not able to run due to the typescript issue

Environment info

Run react-native info in your terminal and copy the results here. Also, include the precise version number of this library that you are using in the project

React native info output:

System:
    OS: macOS 12.3
    CPU: (8) x64 Apple M1
    Memory: 23.13 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.15.1 - ~/.nvm/versions/node/v14.15.1/bin/node
    Yarn: 1.22.17 - ~/.yarn/bin/yarn
    npm: 8.1.1 - ~/.nvm/versions/node/v14.15.1/bin/npm
    Watchman: 2022.03.14.00 - /opt/homebrew/bin/watchman
  Managers:
    CocoaPods: 1.11.2 - /usr/local/bin/pod
  SDKs:
    iOS SDK:
      Platforms: DriverKit 21.4, iOS 15.4, macOS 12.3, tvOS 15.4, watchOS 8.5
    Android SDK:
      API Levels: 28, 29, 30, 31
      Build Tools: 28.0.3, 29.0.2, 30.0.2, 31.0.0
      System Images: android-25 | Google APIs ARM 64 v8a, android-28 | Google APIs Intel x86 Atom, android-29 | Google APIs ARM 64 v8a, android-30 | Google APIs ARM 64 v8a, android-31 | Google APIs ARM 64 v8a, android-31 | Google Play ARM 64 v8a
      Android NDK: Not Found
  IDEs:
    Android Studio: 4.2 AI-202.7660.26.42.7486908
    Xcode: 13.3/13E113 - /usr/bin/xcodebuild
  Languages:
    Java: 15.0.2 - /usr/bin/javac
  npmPackages:
    @react-native-community/cli: Not Found
    react: 17.0.2 => 17.0.2 
    react-native: 0.67.2 => 0.67.2 
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Library version: 12.1.0

Steps To Reproduce

create a react native builder bob package with expo (JS/TS only)
grab a svg from web and use SVGR to create a react native component
copy the output from SVGR playground to a new component on the example app
the TS error should be displayed

Describe what you expected to happen:

  1. Run the app and show the icon
import Svg, { Path, SvgProps } from 'react-native-svg';

import React from 'react';
import { memo } from 'react';

const SvgComponent = (props: SvgProps) => (
  <Svg viewBox="0 0 72 72" {...props} fill="none">
    <Path
      fill={props.fill}
      d="M60 68H12v-8c0-13.25 10.75-24 24-24s24 10.75 24 24v8ZM36 32c7.732 0 14-6.268 14-14S43.732 4 36 4s-14 6.268-14 14 6.268 14 14 14Z"
    />
  </Svg>
);

const Memo = memo(SvgComponent);
export default Memo;

Screen Shot 2022-04-08 at 11 20 21

@eps1lon
Copy link

eps1lon commented Apr 8, 2022

This works fine in the TypeScript Playground.

Does this error also occur when running tsc/building/type-checking the project or is it only visible in the IDE. If the error is only visible in your IDE, which IDE are you using? If not, could you share your tsconfig or, ideally, a minimal, cloneable repository?

@caiorrsdeg
Copy link
Author

caiorrsdeg commented Apr 8, 2022

Hey, that's VSCode, type checking the project. It's a private repo, tomorrow I can give it another try and provide a repo if the issue persists.
tsconfig

{
  "compilerOptions": {
    "baseUrl": "./",
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "esModuleInterop": true,
    "importsNotUsedAsValues": "error",
    "forceConsistentCasingInFileNames": true,
    "jsx": "react",
    "lib": ["esnext"],
    "module": "esnext",
    "moduleResolution": "node",
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "noImplicitUseStrict": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "strict": true,
    "target": "esnext"
  }
}

package.json

"devDependencies": {
    "@commitlint/config-conventional": "^11.0.0",
    "@react-native-community/eslint-config": "^2.0.0",
    "@release-it/conventional-changelog": "^2.0.0",
    "@types/jest": "^27.4.0",
    "@types/lodash": "^4.14.180",
    "@types/react": "^17.0.38",
    "@types/react-native": "0.66.15",
    "@types/vanilla-masker": "^1.2.1",
    "commitlint": "^11.0.0",
    "eslint": "^7.2.0",
    "eslint-config-prettier": "^7.0.0",
    "eslint-plugin-prettier": "^3.1.3",
    "husky": "^6.0.0",
    "jest": "^27.4.7",
    "pod-install": "^0.1.31",
    "prettier": "^2.5.1",
    "react": "17.0.2",
    "react-native": "0.67.2",
    "react-native-builder-bob": "^0.18.0",
    "release-it": "^14.2.2",
    "typescript": "^4.5.5",
    "watch": "^1.0.2"
  },
  "peerDependencies": {
    "react": "*",
    "react-native": "*"
  },
  "dependencies": {
    "axios": "^0.26.1",
    "lodash": "^4.17.21",
    "moment": "^2.29.1",
    "moment-timezone": "^0.5.34",
    "react-native-modal": "^13.0.1",
    "react-native-reanimated": "^2.3.0",
    "react-native-svg": "^12.1.1",
    "vanilla-masker": "^1.2.0"
  }
  

New and slightly different error

image

@caiorrsdeg
Copy link
Author

Maybe some more data. At build time I get this other error (But I think I should post it on that PR thread, right? does not seem to be related to svg at all)

Screen Shot 2022-04-08 at 18 40 33

@rickafds
Copy link

rickafds commented Apr 8, 2022

@caiorrsdeg try, resolved my problem facebook/react#24304 (comment)

@caiorrs
Copy link

caiorrs commented Apr 8, 2022

Reproduceable sample project https://github.com/caiorrs/react-types-error
I will close the issue because it is not related to react native svg

@caiorrsdeg
Copy link
Author

@caiorrsdeg try, resolved my problem facebook/react#24304 (comment)

thanks man, it worked, did not saw it before

@eps1lon
Copy link

eps1lon commented Apr 9, 2022

If you're seeing "SomeComponent cannot be used as a JSX component." after installing React 18 types make sure to only have a single version of @types/react installed.
Try to dedupe it first by removing it's lockfile entry and running npm/yarn again.
If you still see duplicate versions of @types/react (yarn why @types/react or npm ls @types/react) try resolutions with yarn or npm-force-resolutions with npm.

The issue is not caused by a specific version of @types/react but having types for 18 and before 18 installed at the same time.

@brennanbutler01
Copy link

If you're seeing "SomeComponent cannot be used as a JSX component." after installing React 18 types make sure to only have a single version of @types/react installed. Try to dedupe it first by removing it's lockfile entry and running npm/yarn again. If you still see duplicate versions of @types/react (yarn why @types/react or npm ls @types/react) try resolutions with yarn or npm-force-resolutions with npm.

The issue is not caused by a specific version of @types/react but having types for 18 and before 18 installed at the same time.

@eps1lon Thanks so much for this.
Updating my package.json with: "resolutions": { "@types/react": "18.0.1" } and running yarn install again fixed the error.

@CharlesPereira1
Copy link

If you're seeing "SomeComponent cannot be used as a JSX component." after installing React 18 types make sure to only have a single version of @types/react installed. Try to dedupe it first by removing it's lockfile entry and running npm/yarn again. If you still see duplicate versions of @types/react (yarn why @types/react or npm ls @types/react) try resolutions with yarn or npm-force-resolutions with npm.

The issue is not caused by a specific version of @types/react but having types for 18 and before 18 installed at the same time.

Thanks my friend!!!!

lindboe added a commit to infinitered/ignite-cookbook that referenced this issue Feb 15, 2024
Multiple type versions lead to type errors like this one
software-mansion/react-native-svg#1741
lindboe added a commit to infinitered/ignite-cookbook that referenced this issue Feb 21, 2024
* Update for docusaurus v3 versions

* Fix non-terminating image, I think this is the intended one

* Fixing prism post-upgrade

- Requires more languages to be listed, not automatically included now
  - Markup-templating required by ejs, must specify explicitly or build
    breaks
- Update some language references in code blocks that aren't actually
  supported by prism

* Fix observer crash when navigating from homepage

* Update last plugin and clsx

* Actual fix for useIntersection

* Force a single resolution of react types

Multiple type versions lead to type errors like this one
software-mansion/react-native-svg#1741

* Leave comment that gtag is not meant to work in dev

* Convert config to real typescript

* Re-swizzle DocItem/Footer as TS and add more fixes

* Type fixes and disallow unchecked indexed access

* Fix snippet typing issue with home page top bar

* Add a note about swizzling

* Don't emit on typecheck
cdanwards pushed a commit to infinitered/ignite-cookbook that referenced this issue Feb 28, 2024
* Update for docusaurus v3 versions

* Fix non-terminating image, I think this is the intended one

* Fixing prism post-upgrade

- Requires more languages to be listed, not automatically included now
  - Markup-templating required by ejs, must specify explicitly or build
    breaks
- Update some language references in code blocks that aren't actually
  supported by prism

* Fix observer crash when navigating from homepage

* Update last plugin and clsx

* Actual fix for useIntersection

* Force a single resolution of react types

Multiple type versions lead to type errors like this one
software-mansion/react-native-svg#1741

* Leave comment that gtag is not meant to work in dev

* Convert config to real typescript

* Re-swizzle DocItem/Footer as TS and add more fixes

* Type fixes and disallow unchecked indexed access

* Fix snippet typing issue with home page top bar

* Add a note about swizzling

* Don't emit on typecheck
cdanwards pushed a commit to infinitered/ignite-cookbook that referenced this issue Feb 28, 2024
* Update for docusaurus v3 versions

* Fix non-terminating image, I think this is the intended one

* Fixing prism post-upgrade

- Requires more languages to be listed, not automatically included now
  - Markup-templating required by ejs, must specify explicitly or build
    breaks
- Update some language references in code blocks that aren't actually
  supported by prism

* Fix observer crash when navigating from homepage

* Update last plugin and clsx

* Actual fix for useIntersection

* Force a single resolution of react types

Multiple type versions lead to type errors like this one
software-mansion/react-native-svg#1741

* Leave comment that gtag is not meant to work in dev

* Convert config to real typescript

* Re-swizzle DocItem/Footer as TS and add more fixes

* Type fixes and disallow unchecked indexed access

* Fix snippet typing issue with home page top bar

* Add a note about swizzling

* Don't emit on typecheck
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants