xxxxxxxxxx
//Use this WEB APP to convert any svg to a valide reactJs or React native Component
Playground - SVGR
https://react-svgr.com/playground/
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
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
import { ReactComponent as IconMenu } from './icons/menu.svg'
<IconMenu />
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" />
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>