Reactjs Resact onScroll不从父组件触发
我有一个父组件,它最初没有高度,但会随着子组件的添加而增长。我可以在添加子项后上下滚动,但我注意到handleScroll函数没有启动 有人有类似的问题吗Reactjs Resact onScroll不从父组件触发,reactjs,scroll,Reactjs,Scroll,我有一个父组件,它最初没有高度,但会随着子组件的添加而增长。我可以在添加子项后上下滚动,但我注意到handleScroll函数没有启动 有人有类似的问题吗 constructor() { super(); this.handleScroll = this.handleScroll.bind(this); } handleScroll(e) { console.log(e); } render() { return ( <div className="parent"
constructor() {
super();
this.handleScroll = this.handleScroll.bind(this);
}
handleScroll(e) {
console.log(e);
}
render() {
return (
<div className="parent" onScroll={this.handleScroll}>
{
this.props.children.map((child)=>{
<div>{child.name}</div>
})
}
)
}
constructor(){
超级();
this.handleScroll=this.handleScroll.bind(this);
}
handleScroll(东){
控制台日志(e);
}
render(){
返回(
{
this.props.children.map((child)=>{
{child.name}
})
}
)
}
您可以通过以下方式将滚动功能绑定到div:
import ReactDOM from 'react-dom';
// ...
constructor() {
super();
this.handleScroll = this.handleScroll.bind(this);
}
componentDidMount() {
const div = ReactDOM.findDOMNode(this.refs.someRef)
div.addEventListener('scroll', this.handleScroll);
}
componentWillUnmount() {
const div = ReactDOM.findDOMNode(this.refs.someRef)
div.removeEventListener('scroll', this.handleScroll);
}
handleScroll(e) {
console.log(e);
}
render() {
return (
<div ref="someRef" className="parent">
{
this.props.children.map((child)=>{
<div>{child.name}</div>
})
}
</div>
)
}
您可以通过以下方式将滚动功能绑定到div:
import ReactDOM from 'react-dom';
// ...
constructor() {
super();
this.handleScroll = this.handleScroll.bind(this);
}
componentDidMount() {
const div = ReactDOM.findDOMNode(this.refs.someRef)
div.addEventListener('scroll', this.handleScroll);
}
componentWillUnmount() {
const div = ReactDOM.findDOMNode(this.refs.someRef)
div.removeEventListener('scroll', this.handleScroll);
}
handleScroll(e) {
console.log(e);
}
render() {
return (
<div ref="someRef" className="parent">
{
this.props.children.map((child)=>{
<div>{child.name}</div>
})
}
</div>
)
}
您的
div.parent的样式是什么?它是否包含溢出:滚动的属性
渲染时确实溢出了吗?您的div.parent的样式是什么?它是否包含溢出:滚动的属性代码>渲染时确实溢出了吗?