是否有任何方法可以获得封装组件的DOM高度?
我试图添加一个引用,但控制台错误了我
Function components cannot be given refs.
我设置了
forward ref
但情况似乎并非如此。
export default function withInfiniteScroll(Component) {
return class extends React.Component {
componentDidMount() {
window.addEventListener('scroll', this.onScroll, true);
}
onScroll = () => {
// here
console.log(
'window.innerHeightð', window.innerHeight,
'\ndocument.body.offsetHeightð', document.body.offsetHeight,
);
}
render() {
return <Component {...this.props} />;
}
};
}
我想记录
Component
但是这些日志毫无意义,它们是HTML正文的高度,而不是
成分
s。
window.innerHeightð 767
document.body.offsetHeightð 767
但当我在Chrome控制台中时:
console.log(document.getElementsByClassName('home-container')[0].clientHeight)
> 1484
哪一个
'home-container'
是包装组件:
withInfiniteScroll(HomeContainer);