Reactjs 尝试在react中使用scrollHeight

Reactjs 尝试在react中使用scrollHeight,reactjs,height,Reactjs,Height,有人能告诉我为什么我在使用console.log heightSet时没有定义吗 componentDidMount() { this.updateDimensions(); window.addEventListener('resize', this.updateDimensions.bind(this)); } componentWillUnmount() { window.removeEventListener('resize', this.updateDimen

有人能告诉我为什么我在使用console.log heightSet时没有定义吗

componentDidMount() {
    this.updateDimensions();
    window.addEventListener('resize', this.updateDimensions.bind(this));
}

componentWillUnmount() {
    window.removeEventListener('resize', this.updateDimensions.bind(this));
}

updateDimensions() {
    this.setState({ heightSet: document.scrollHeight });
    console.log(document.clientHeight);
}
相同的console.log适用于window.innerHeight(clientHeight也未定义),但我不需要内部高度,我需要scrollHeight


谢谢

滚动高度
元素
上的一个属性,所以我想您可能需要
document.body.scrollHeight

顺便说一下,每次调用
this.updateDimensions.bind(this)
都会得到一个新函数,因此不能使用这种方法添加和删除同一个事件侦听器

您应该向类中添加一个构造函数,其中包含以下内容:

constructor(props) {
    super(props);
    this.updateDimensions = this.updateDimensions.bind(this);
}
然后您可以添加/删除此。直接更新维度:

componentDidMount() {
    this.updateDimensions();
    window.addEventListener('resize', this.updateDimensions);
}

componentWillUnmount() {
    window.removeEventListener('resize', this.updateDimensions);
}

updateDimensions() {
    this.setState({ heightSet: document.body.scrollHeight });
}

您可能希望尝试使用“滚动”事件

componentDidMount() {
    this.updateDimensions();
    window.addEventListener('scroll', this.updateDimensions);
}

componentWillUnmount() {
    window.removeEventListener('scroll', this.updateDimensions);
}

updateDimensions() {
    this.setState({ heightSet: window.scrollY });
}

哦,这并不是它未定义的原因。那里更新。我已经添加了完整的结构,或者其他的部分现在都在那里。我能看到的主要是一个越来越复杂的需求。我看到的一个直接问题是,您正在订阅窗口调整大小事件,这与向容器中添加元素无关。这是我的假设。毕竟,在我看来,从技术上讲,他们是那种财产的“孩子”,为什么不呢。我可以用什么方法来解决这个问题?