dom上如何挂载react组件
import ReactDOM from 'react-dom';
import { Component } from 'react';
class Com extends Component {
componentWillUnmount() {
ReactDOM.unmountComponentAtNode(this.props.unmountDom); //组件卸载时,去除当前实例
}
render() {
return <div>组件</div>
}
}
const dom = document.querySelectorAll('.classname');
if(dom.length > 0){
ReactDOM.render(<Com unmountDom = {dom} />, dom[0]);
}
去除dom上挂载的实例,可以在react组件卸载时调用,防止内存泄漏
ReactDOM.unmountComponentAtNode(dom);