DEV Community

Cover image for How to fix the regular error using react-icons in React with Typescript?
Sahba Rezvani
Sahba Rezvani

Posted on

How to fix the regular error using react-icons in React with Typescript?

Starting from version 19, the typings for @types/react introduced a major change!
Now, by default, the JSX namespace is no longer available, and you have to explicitly define it yourself.

🧠 Why did this happen?
In the new versions of TypeScript and React typings:

JSX.Element is not available directly unless you properly configure your environment.

If your project is old, or if your tsconfig.json file is missing some settings, TypeScript will not be able to recognize JSX.

πŸ’‘ Solution 1 (Quick and Easy):

Go to your project’s tsconfig.json file.
Make sure you have these important options configured like this:

{
  "compilerOptions": {
    "jsx": "react-jsx",
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "module": "esnext",
    "target": "esnext",
    ...
  }
}
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ The most important part is:
"jsx": "react-jsx"

πŸ’‘ Solution 2 (Definitive and 100% Working):

Create a new file called global.d.ts at the root of your project.
Inside it, add the following lines:

/// <reference types="react" />
/// <reference types="react-dom" />

Enter fullscreen mode Exit fullscreen mode

This tells the project:
"Hey buddy, grab the JSX namespace from React!"

But of course before all these, you need to check if these two are installed in your project:

npm install --save react react-dom
npm install --save-dev typescript @types/react @types/react-dom

so the error you get is clearly because of your project's typing and not because of the way you're using the react icons!

Hope it helps❀
And if it does, please do like, share and leave me a comment.

Top comments (1)

Collapse
 
jamesdayal99 profile image
James

Can you accept my invitation so that I can get a free gift?
temu.com/u/bzS4dzGS5i1Xt
The person who will install it and sign up with this Referral link can buy everything here