我正在使用标签渲染SVG组件.这些标签组件需要根据其文本内容(以及它们的大小)正确布局,以避免重叠.
为了获得每个标签的实际大小,每次更新标签内容时都需要双重渲染.
在标签组件级别,我需要
然后,在每次重绘时:
到目前为止,这是我如何实现我的标签组件:
var Label = React.createClass({
updateBBox: function() {
// Trigger re-rendering
this.setState({
bbox: this.getDOMNode().getBBox()
});
},
componentDidMount: function() {
// Will trigger a re-rendering at mount
this.updateBBox();
},
componentDidUpdate: function(prevProps, prevState) {
// If content has changed, re-render
if (this.props.content !== prevProps.content) {
this.updateBBox();
}
},
render: function() {
// Render according to size from current bounding box if any cached
// ... …Run Code Online (Sandbox Code Playgroud)