在 React 中是否有类似于 jQuery 的 .empty() 的函数?



这是一个边缘情况。我正在尝试使用一个插件,该插件需要您在每个更新上创建一个New实例。我想知道React是否提供了一种辅助方法来在收到新的道具时清空组件的DOM。

以下是澄清的代码示例:

export default React.createClass({
  displayName: 'Item',
  contextTypes: {
    router: React.PropTypes.func
  },
  propTypes: {
    items: React.PropTypes.object.isRequired
  },
  getInitialState() {
    return AppStore.getState();
  },
  componentDidMount() {
    this.renderSVG();
  },
  componentWillReceiveProps() {
    this.renderSVG();
  },
  renderSVG() {
    const {id}: string = this.context.router.getCurrentParams();
    this.item = this.props.items[id];
    this.classString = 'svg-container-' + this.item.id;
    // TODO: find a solution for side-effects linting error
    new Vivus(React.findDOMNode(this), {type: 'delayed', duration: 100, file: this.item.url});
  },
  render() {
    return (
      <div className={this.classString}></div>
    );
  }
});

这实现了所需的效果。

 componentWillReceiveProps() {
    React.findDOMNode(this).removeChild(React.findDOMNode(this).childNodes[0]);
    this.renderSVG();
  },

最新更新