如果你这样写,页面跳转后在回来 会发现 之前的清除监听未能生效;造成重复监听bug;
componentDidMount() {
document.addEventListener('keydown', _.debounce(this.handleKeydown))
}
componentWillUnmount() {
document.removeEventListener('keydown', .debounce(this.handleKeydown))
}
你应该如下写法
constructor(props){
super(props)
this.handleKeydown= _.debounce(this.handleKeydown, 100)
}
componentDidMount() {
document.addEventListener('keydown', this.handleKeydown)
}
componentWillUnmount() {
document.removeEventListener('keydown', this.handleKeydown)
}