react > 16.3
尝试在此组件中使用门户:
import {Component} from 'react';
import {createPortal} from 'react-dom';
import PropTypes from 'prop-types';
class DOMPortal extends Component {
    constructor(props) {
        super(props);
        this.el = document.createElement(props.component);
    }
    componentDidMount() {
        this.props.parentEl.appendChild(this.el);
    }
    componentWillUnmount() {
        this.props.parentEl.removeChild(this.el);
    }
    render() {
        return createPortal(
            this.props.children,
            this.el,
        );
    }
}
DOMPortal.propTypes = {
    parentEl: PropTypes.object.isRequired,
    component: PropTypes.string,
};
DOMPortal.defaultProps = {
    component: 'div',
};
现在您可以将外部 DOM 引用作为 parentEl props传递给它:
<DOMPortal parentEl={decorator.contentWidget.domNode}>...children</DOMPortal>
react < 16.3
使用this.refs是“弃用”,试试这个:
render() {
 return <div ref={(DOMNodeRef) => {
                 this.componentRef=DOMNodeRef;
                }}>
      ...
      </div>;
 }
然后this.componentRef将可以访问,componentDidMount()以便您可以附加外部 DOM 元素:
componentDidMount(){
  this.componentRef.appendChild(externalDOMelement);
}
笔记:
请记住,它this.componentRef会随着时间而变化(renders()),因此您必须在将它传递到的任何地方更新它。
在使用之前检查定义的引用: if(this.componentRef){// ... your code}
功能组件的引用以不同方式处理。
来源:
react文档