Normally I'd use material-ui icons by importing them directly per the material-ui instructions.
But I have a text tag, which is the actual icon name (like calendar_view_day
) and need to get and render an icon component from it dynamically.
How can I something like:
render() {
return (
<Icon name="calendar_view_day" color="primary" />
)
}
Instead of:
render() {
return (
<CalendarViewDay color="primary" /> // Imported by name
)
}
If you don't use Material UI in your project yet, install the icons package with npm install @mui/icons-material @mui/material @emotion/styled @emotion/react . See the Installation page for additional docs about how to make sure everything is set up correctly.
Create an icon buttonImport the IconButton component from the Material-UI core package. import IconButton from '@material-ui/core/IconButton'; Render the icon as a child component to the IconButton . You can also move the color prop to the IconButton .
OK, so I massively overthought this.
Turns out material-ui
includes an icon component that allows you to do this... and it converts names itself, so accepts snake, pascal and other variants. BEWARE this will massively increase bundle size, as pointed out in the comments. If you're bundle size constrained, you'll have to take a different approach of serving the icon SVGs from somewhere!
import Icon from '@material-ui/core/Icon'
...
render() {
return (
<Icon>{props.iconName}</Icon>
)
}
Create function to:
...Then use the material-ui Icon component.
Here's the code:
import Icon from '@material-ui/core/Icon'
function upperFirst(string) {
return string.slice(0, 1).toUpperCase() + string.slice(1, string.length)
}
function fixIconNames(string) {
const name = string.split('_').map(upperFirst).join('')
if (name === '3dRotation') {
return 'ThreeDRotation'
} else if (name === '4k') {
return 'FourK'
} else if (name === '360') {
return 'ThreeSixty'
}
return name
}
...
render() {
const iconName = fixIconNames(props.iconName)
return (
<Icon>{props.iconName}</Icon>
)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With