xxxxxxxxxx
<span>
<object style={{ float: 'left', height: '15px', position: 'relative', top: '4px' }} data={`${ROOT_URL}/assets/images/svg-icons/history-icon.svg`} type='image/svg+xml'></object>
</span>
xxxxxxxxxx
import React from 'react';
import { ReactComponent as BrandIcon } from "./assets/brand-icon.svg";
export default function(){
return(
<div>
<BrandIcon />
</div>
);
}
xxxxxxxxxx
# with npm
npm i vite-plugin-svgr
Next, add the plugin inside your app's vite.config.js:
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import svgr from "vite-plugin-svgr";
// https://vitejs.dev/config/
export default defineConfig({
plugins: [svgr(), react()],
});
import { ReactComponent as Logo } from "./logo.svg";
xxxxxxxxxx
import React from 'react';
import LogoName from '../assets/logo.svg'
const Header = () => {
return (
<div>
<img src={LogoName} alt="logo"/>
</div>
);
}
xxxxxxxxxx
//customIcon.js
import React from "react";
import {ReactComponent as ImportedSVG} from "path/to/myIcon.svg";
import { SvgIcon } from '@material-ui/core';
function CustomIcon() {
return(
<SvgIcon component={ImportedSVG} viewBox="0 0 384 512"/>
)
}
export default CustomIcon;
xxxxxxxxxx
import React from 'react';
import {ReactComponent as ReactLogo} from './logo.svg';
const App = () => {
return (
<div className="App">
<ReactLogo />
</div>
);
}
export default App;
xxxxxxxxxx
import React from 'react';
import { ReactComponent as YourSvg } from './YourSvg.svg';
const MyComponent = () => {
return(
<YourSvg/>
);
}
export default MyComponent ;
xxxxxxxxxx
import YourSvg from "/path/to/image.svg";
const App = () => {
return (
<div className="App">
<img src={YourSvg} alt="Your SVG" />
</div>
);
};
export default App;
xxxxxxxxxx
If you use create-react-app 2.0 you can now do it like this:
import { ReactComponent as YourSvg } from './your-svg.svg';
And then use it just like you would normally use a component:
const App = () => (
<div>
<YourSvg className="YourClassName" style={{fill: "#fff"}} />
</div>
);
xxxxxxxxxx
// How to add SVGs in React?
// There are 4 diff. ways to add SVGs to React here is the easiest
// save your file, ex.:chatbot.svg, in src/assets
// then import it in the file where you want it rendered:
import chatbot from './assets/chatbot.svg'
render (
<div>
<img src={chatbot} alt:='chatbot' />
</div>
)
// Boom, Done. nothing else to do.
xxxxxxxxxx
I recommend you to use react-bootstrap.
It works if you also have a Nextjs Project.
https://react-bootstrap.netlify.app/components/images/
import React from "react";
import Image from "react-bootstrap/Image";
// If the svg is in public
<Image src="../../svg.svg" />
// or
<Image src="../public/svg.svg" />