xxxxxxxxxx
moduleNameMapper: {
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': '<rootDir>/src/utils/file-mock.js',
'^.+\\.(css|less|scss)$': 'babel-jest'
}
//../src/utils/file-mock.js
module.exports = 'stubbed-file';
xxxxxxxxxx
import React from 'react';
import LogoName from '../assets/logo.svg'
const Header = () => {
return (
<div>
<img src={LogoName} alt="logo"/>
</div>
);
}
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
<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>