Later Edits
EDIT 2: Look at @Roozbeh's more up-to-date mid-2023 answer on using Font Material Icon before using the following.
EDIT 1: This answer is for MUI v4, MUI v5 answers below (but untested by me)
Correct answer
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>
)
}
Previous answer (working but massive overkill)
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>
)
}
<span />
and not a<svg />
. This may not be a big deal in a lot of cases but sadly a deal breaker for my use case. – Amoral