Browse Source

fix event listener not removed

dev
xmcp 6 years ago
parent
commit
38cc91ad9b
  1. 21
      src/UserAction.js

21
src/UserAction.js

@ -270,17 +270,22 @@ export class ReplyForm extends Component {
}; };
this.on_change_bound=this.on_change.bind(this); this.on_change_bound=this.on_change.bind(this);
this.area_ref=this.props.area_ref||React.createRef(); this.area_ref=this.props.area_ref||React.createRef();
this.global_keypress_handler_bound=this.global_keypress_handler.bind(this);
} }
componentDidMount() { global_keypress_handler(e) {
document.addEventListener('keypress',(e)=>{ if(e.code==='Enter' && !e.ctrlKey && !e.altKey && ['input','textarea'].indexOf(e.target.tagName.toLowerCase())===-1) {
if(e.code==='Enter' && !e.ctrlKey && !e.altKey && ['input','textarea'].indexOf(e.target.tagName.toLowerCase())===-1) { if(this.area_ref.current) {
if(this.area_ref.current) { e.preventDefault();
e.preventDefault(); this.area_ref.current.focus();
this.area_ref.current.focus();
}
} }
}); }
}
componentDidMount() {
document.addEventListener('keypress',this.global_keypress_handler_bound);
}
componentWillUnmount() {
document.removeEventListener('keypress',this.global_keypress_handler_bound);
} }
on_change(value) { on_change(value) {

Loading…
Cancel
Save