M
M
m_frost2019-06-13 21:41:10
React
m_frost, 2019-06-13 21:41:10

React refs and element height???

The rest of the chat window, I want to push the scroll to the bottom of the component and use react.React.createRef() to create this.windowRef.scrollTop = this.windowRef.scrollHeight on didMount and DidUpdate

constructor(props) {
    super(props);
    this.windowRef = React.createRef();
  }

  componentDidMount() {
    const wind = document.querySelector('#window');
    console.log(wind.scrollHeight);
    this.windowRef.scrollTop = this.windowRef.scrollHeight;
  }

  componentDidUpdate() {
    this.windowRef.scrollTop = this.windowRef.scrollHeight;
    console.log(this.windowRef.scrollHeight);
  }

by id everything works but with ref error
Cannot add property scrollTop, object is not extensible
and even this.windowRef.scrollHeight = underfied

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Robur, 2019-06-13
@m_frost

console.log(this.windowRef.current.scrollHeight)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question