Yes, you can use SVG icons with Material-UI in React. Material-UI provides several ways to use SVG icons:
1. Material Icons:
- Material-UI comes with a set of over 2,100 Material Icons that are ready to use as React components. These icons are exported from the `@mui/icons-material` package and can be imported directly into your React components[1][3].
2. SvgIcon Component:
- The `SvgIcon` component is a wrapper for custom SVG icons. It extends the native `
3. Custom SVG Icons:
- You can import SVG files directly into your React components using loaders like `@svgr/webpack` or `url-loader` in your `webpack.config.js` file. This method allows you to use SVG icons without any additional libraries[1][2].
Here is an example of using the `SvgIcon` component with a custom SVG icon:
javascript
import { ReactComponent as StarIcon } from './star.svg';
import SvgIcon from '@material-ui/core/SvgIcon';
const Icon = () => {
return (
<SvgIcon component={StarIcon} inheritViewBox />
);
};
export default Icon;
In this example, the `SvgIcon` component wraps the custom SVG icon (`StarIcon`) and inherits the `viewBox` attribute from the original SVG file.
Installation
To use Material Icons, you need to install the required packages:
bash
npm install @mui/icons-material @mui/material @emotion/styled @emotion/react
Usage
To use Material Icons, you can import them directly into your React components:
javascript
import AccessAlarmIcon from '@mui/icons-material/AccessAlarm';
Custom SVG Icons
To use custom SVG icons, you need to configure your Webpack setup to handle SVG files. For example, you can use the `@svgr/webpack` loader:
javascript
module.exports = {
// ... other configurations
module: {
rules: [
{
test: /\.svg$/,
use: ['@svgr/webpack'],
},
],
},
};
Then, you can import your SVG icons directly:
javascript
import { ReactComponent as StarIcon } from './star.svg';
Conclusion
Material-UI provides robust support for SVG icons, allowing you to use both Material Icons and custom SVG icons in your React applications. The `SvgIcon` component is a versatile tool for wrapping custom SVG icons and integrating them seamlessly with Material-UI components.
Citations:[1] https://mui.com/material-ui/icons/
[2] https://trailheadtechnology.com/mui-custom-icons-creation-from-svg-sprite-in-react-development/
[3] https://mui.com/material-ui/material-icons/
[4] https://www.npmjs.com/package/%40material-ui/icons
[5] https://stackoverflow.com/questions/38510443/how-to-use-an-svg-file-in-a-svgicon-in-material-ui