bey*_*ddc 9 reactjs react-bootstrap
我按照React.js中的帖子 更新样式onScroll上的指令来注册滚动事件的事件监听器.
我有一个React组件,它从React-Bootstrap库中呈现一个Table组件https://react-bootstrap.github.io/
我想我正确地注册了事件监听器,但我不确定为什么当我向下滚动表时,我的handleScroll()回调没有被调用.是因为事件监听器没有在实际表本身上注册吗?
感谢您花时间阅读我的问题.任何反馈都表示赞赏.
这是我如何注册事件监听器的片段.
handleScroll: function(event) {
console.log('handleScroll invoked');
},
componentDidMount: function() {
console.log('componentDidMount invoked');
window.addEventListener('scroll', this.handleScroll);
},
componentWillUnmount: function() {
console.log('componentWillUnmount invoked');
window.removeEventListener('scroll', this.handleScroll);
},
Run Code Online (Sandbox Code Playgroud)
这是我的渲染功能的片段.
render: function() {
var tableRows = this.renderTableRow(this.props.sheet);
return (
<Table striped bordered condensed hover>
<TableHeaderContainer
tableTemplateName={this.props.tableTemplateName}
sheetName={this.props.sheet.name}/>
<tbody>
{tableRows}
</tbody>
</Table>
);
}
Run Code Online (Sandbox Code Playgroud)
dan*_*lie 14
你的代码看起来不错,所以它可能不是滚动的窗口本身.桌子放在一个div或有什么东西overflow: auto或overflow:scroll?如果是这样,则必须将侦听器附加到滚动的实际元素,例如
document.querySelector('.table-wrapper')
.addEventListener('scroll', this.handleScroll);
Run Code Online (Sandbox Code Playgroud)
如果是这种情况,那么只需将React onScroll处理程序添加到代码中的包装器就更好了
<div onScroll={this.handleScroll}><Table....
Run Code Online (Sandbox Code Playgroud)
car*_*kod 10
对我来说,唯一有效的方法是添加true到第三个参数中EventListener:
componentDidMount() {
window.addEventListener('scroll', this.handleScroll, true);
}
componentWillUnmount() {
window.removeEventListener('scroll', this.handleScroll, true);
}
Run Code Online (Sandbox Code Playgroud)
来源:https : //github.com/facebook/react/issues/5042#issuecomment-145317519
| 归档时间: |
|
| 查看次数: |
11659 次 |
| 最近记录: |