Replies: 6 comments 7 replies
-
Another fun thing I'm seeing with this scenario is that parcel will just get stuck spinning with "Building index.js...." until I quit. As far as the types go, it looks like you can add this to a .d.ts file to fix the import declare module "*.svg" {
import * as React from "react";
const ReactComponent: React.FunctionComponent<
React.SVGProps<SVGSVGElement> & { title?: string }
>;
export default ReactComponent;
} |
Beta Was this translation helpful? Give feedback.
-
I have discovered a workaround to get svg typescript generation working: #8376 |
Beta Was this translation helpful? Give feedback.
-
You should try including this in your
And then add After these steps, you should actually import any SVG as a ReactComponent like this:
|
Beta Was this translation helpful? Give feedback.
-
Just create a new Component and return the svg as html elements. Pass down className if needed. import React from "react";
interface SearchIconAttributes {
className?: string,
}
export const SearchIcon: React.FC<SearchIconAttributes> = ({ className = '' }: SearchIconAttributes) => {
return (
<svg className={className} xmlns="http://www.w3.org/2000/svg" viewBox={`0 0 48 48`} >
<path d="M 20.5 6 C 12.515556 6 6 [...] 10 20.5 10 z" />
</svg>
);
} This is just copied from one of my projects, and it's working perfectly fine. No tedious declaration file shenanigans, or using custom file loaders. |
Beta Was this translation helpful? Give feedback.
-
add a file with .d.ts extension, i used image.d.ts, put in src. declare module '*.svg' { make sure ./src is in your include in ts.config. should also have the dom types there too, "lib": ["DOM", "DOM.Iterable", "ESNext"], can then import like: import AvatarImage from '../../../assets/img/[email protected]'; |
Beta Was this translation helpful? Give feedback.
-
For NextJS ⛷️Configuration for SVG Handling in Next.js 🚀I found the following configuration to work seamlessly in Next.js: //next.config.mjs
export default {
webpack: (config) => {
// Add rule for SVG files
config.module.rules.push({
test: /\.svg$/,
use: ['@svgr/webpack', 'url-loader'],
});
return config;
},
reactStrictMode: true,
}; Please note that you need to install @svgr/webpack and url-loader with npm or yarn to use them in your Webpack configuration. npm install --save-dev @svgr/webpack url-loader
# or
yarn add --dev @svgr/webpack url-loader And use it like this 😎import React from "react";
import { ReactComponent as MailIcon } from "@assets/icons/auth/mail.svg";
function Page() {
return (
<div>
{/* Utilizing the configured MailIcon component */}
<MailIcon strokeWidth={1.2} />
</div>
);
}
export default Page; Fixing SVG Import Issue in TypeScript 🛠️To resolve the issue, declare a module for SVG files in your TypeScript configuration. This example uses the following code: // svg.d.ts
declare module "*.svg" {
import React from "react";
export const ReactComponent: React.FunctionComponent<React.SVGProps<SVGSVGElement>>;
} For ReactJs 🏊Integrating SVG as React Component in React Application 🚀To seamlessly use SVG files as React components in your React application, follow these steps: Install Necessary PackagesYou need to install npm install --save-dev @svgr/webpack url-loader Create a webpack configuration file (e.g., webpack.config.js) and add the following code: // webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.svg$/,
use: ['@svgr/webpack', 'url-loader'],
},
],
},
}; Now, you can import SVG files and use them as React components in your application. For example: import { ReactComponent as Logo } from './path/to/your/logo.svg';
function App() {
return (
<div>
{/* Logo is your SVG as a React component */}
<Logo />
</div>
);
}
export default App; |
Beta Was this translation helpful? Give feedback.
-
Hello, I would like to use some .svg files as React components in my project but I don't know how to realize that and there is no documentation on the topic.
in my .ts file I try
import Icon from "./Icon.svg"
and my.parcelrc
ishowever when I try to build I get
@parcel/transformer-typescript-types: Cannot find module './Icon.svg' or its corresponding type declarations.
I have tried adding a
.svgrc
to the root directory to configure SVGR which this module is running on and set it to{"typescript":true}
but to no avail...I now settled on using SVGR manually to generate react components but I would prefer not to do that for each parcel project I work with.
Did anybody manage to compile SVG with Typescript into react components?
Beta Was this translation helpful? Give feedback.
All reactions