-
Notifications
You must be signed in to change notification settings - Fork 47.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dangerouslySetInnerHTML doesn't update on SVG <g> #2863
Comments
We can confirm the same for |
Worth noting, we're using |
I have a mixin I'm using to change the key on the parent every time the props change to force it to fully re-render. I did modify this before posting because my first version involved keeping the key with // https://github.com/facebook/react/issues/2863
//
// React operates by touching the DOM as little as possible. Whenever it can
// it'll keep existing DOM nodes and just modify attributes. The strategy when
// using `dangerouslySetInnerHTML` is to simply set `.innerHTML` on the DOM
// node. When it comes to <g> (and probably other SVG tags) setting .innerHTML
// doesn't work. But the strategy it uses to create them initially is fine.
//
// On any React element you can pass a key as a unique identifier. If the
// first render says `<g key={1} />` and the next render says `<g key={2} />`
// then react knows that these are backed by different objects and you aren't
// just changing properties. By changing the key every time the image changes
// it forces React to throw away the `<g>` DOM node and create a new one.
//
// http://facebook.github.io/react/docs/multiple-components.html#child-reconciliation
var workaround2863 = {
get2863key: function() {
var nextProps = this.props;
var nextState = this.state;
var lastProps = this._lastProps2863;
var lastState = this._lastState2863;
if (!_.isEqual(lastProps, nextProps)
|| !_.isEqual(lastState, nextState)) {
this._cached2863Key = Date.now();
}
this._lastProps2863 = nextProps;
this._lastState2863 = nextState;
return this._cached2863Key;
},
exampleRender: function() {
var html = '....';
return (
<g
key={this.get2863key()}
dangerouslySetInnerHTML={{__html: html}}
/>
);
}
}; |
I'm pretty sure there's a much simpler workaround: render: function() {
var html = "...";
return (
<g key={html} dangerouslySetInnerHTML={{__html: html}} />
);
} |
I'm currently trying to update
|
Rect is a supported tag. Why are you using dangerouslySetInnerHTML? |
Updating the key manually forced react to rerender the svg and fixed my problem in safari |
Interesting. I ran into that exact problem on Friday. It works as a Style though: <circle
style={{
// passing clipPath as a property didn't work, but
// using a style is fine.
clipPath: "url(#keep-left)"
}} |
good workaround for the clip-path attribute! But I got stuck with the |
For everybody who comes to this issue from now: Example: // This doesn't work in Safari
<svg dangerouslySetInnerHTML={
{ __html: `<path d="${this._getPath(path1)} ${this._getPath(path2)}" fill-rule="evenodd"></path>`}
}></svg>
// This does
<svg>
<path d={this._getPath(path1) + ' ' + this._getPath(path2)} style={fill-rule: evenodd;}></path>
</svg> It even reads better ;) |
My case is a bit different, I download SVG content (images) and inline them this way: render() {
const { content } = this.props
if (!content) return null
return <g dangerouslySetInnerHTML={{ __html: content }}/>
} Dead simple. It works on Chrome and most recent browsers but it doesn't in IE10 nor IE9. I tried the |
Good news everyone! This issue has been fixed few days ago |
I suspect this applies to all SVG elements, but I only know
<g>
for sure. Due to lack of an SVG<image>
I'm needing to usedangerouslySetInnerHTML
. I created a jsfiddle to demonstrate the issue. This will switch between two images of Tux each second. It will also dumpinnerHTML
to the console after each render.Every browser renders correctly initially, but only Chrome can update.
<image>
gets converted to an<img>
and disappears. I assume because it's now an HTML<img>
inside an SVG context.innerHTML
looks fine, but the image never changes.innerHTML
looks fine, but the image never changes.The text was updated successfully, but these errors were encountered: