1.導入react
<div ref="refTest"
</div>
//監聽外部click componentDidMount() { document.addEventListener('mousedown', (e)=>this.handleClickOutside(e), false); } componentWillUnmount() { document.removeEventListener('mousedown', (e)=>this.handleClickOutside(e), false); } handleClickOutside(e) { const target = e.target; console.log(target); console.log(this); // 組件已掛載且事件觸發對象不在div內 let result=findDOMNode(this.refs.refTest).contains(e.target); if( !result) { console.log("ssscs"); this.setState({ ulShow:false }); } }