When attempting to use Material-UI’s SvgIcon
component the <path>
is being surrounded by quotes, causing the SVG to not render.
I am working out of Storybook in an MDX file. To render the SVG I’ve tried a few methods, but they all result in the same output. The most straightforward of these attempts is:
JavaScript
x
8
1
import { accessibility1Icon } from '@cds/core/icon';
2
3
export const Template = (args) => {
4
return (
5
<SvgIcon {args}>{accessibility1Icon[1].outline}</SvgIcon>
6
)
7
}
8
The reference going into <SvgIcon>
is indeed a path
. It does come out (as seen in the image above) but it is surrounded in quotes in the DOM.
What might I be missing that is causing these quotes, or what can be done to retype the reference so they don’t appear?
Advertisement
Answer
Because you cannot render a string
as JSX
,
You have to find a way to convert the string
to JSX
.
1- A solution using dangerouslySetInnerHTML
:
JavaScript
1
10
10
1
import { accessibility1Icon } from '@cds/core/icon';
2
3
export const Template = (args) => {
4
return (
5
<SvgIcon {args}>
6
<g dangerouslySetInnerHTML={{ __html: accessibility1Icon[1].outline }} />
7
</SvgIcon>
8
)
9
}
10
2- A solution using html-react-parser
JavaScript
1
11
11
1
import { accessibility1Icon } from '@cds/core/icon';
2
import parse from 'html-react-parser';
3
4
export const Template = (args) => {
5
return (
6
<SvgIcon {args}>
7
{parse(accessibility1Icon[1].outline)}
8
</SvgIcon>
9
)
10
}
11