我一直在研究SVG和React的数据表示,他们似乎是一个完美的合适。但是,React还不支持SVG,并且还没有插件https://github.com/facebook/react/issues/2250。在React.js中使用SVG过滤器属性
还有一些解决方法,比如对某些元素使用dangerouslySetInnerHTML
,在我的情况下我还找不到一个。
我想在circle
svg元素中使用filter
属性。但它不会呈现到DOM中。这是我的阵营组件:
class DeviceDot extends React.Component {
render() {
const { app, idx } = this.props
return <circle cx={50 * idx} cy={50 * idx} r='25' filter={`url(#${app})`} fill='mediumorchid' />
}
}
和过滤器:
class FilterSVG extends React.Component {
render() {
const { app, idx } = this.props
const icon = app ? `/api/apps/${app}/logo` : '/img/dflt.png'
return (
<filter id={app || 'default'} x='0%' y='0%' width='100%' height='100%'>
<feImage xlinkHref={icon} />
</filter>
)
}
}
但是我不能没有包装这个组件,我想重用的是(圆圈SVG元素)使用dangerouslySetInnerHTML。是否有任何解决方法可以使用现在使用的这个属性(和其他)?
谢谢。
是的,它工作!但仍然只用于“过滤器”属性。也许有其他的解决方法,其他属性不起作用。编辑,如果你发现别的东西。谢谢。 – jsdario