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
)
}
OK, so I massively overthought this.
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!Previous answer (working but massive overkill)
Create function to:
...Then use the material-ui Icon component.
Here's the code: