0

I'm new to react, I would like a div to scroll up as it starts to overflow, and I am trying to do it the proper way with react, but can't seem to figure it out.

I have the following in my render method:

render:function(){

    return (
      <div className="main">
        <div className="title">talkin folk</div>
        <div className="chatBox">
          <div className="chatRooms">
            <p className="large">rooms</p>
            {this.arrayOfRooms}
          </ div>
          <div className="typingBox">
            <div ref='dialog' key='dialog' className="dialog"> // div to scroll
              {this.arrayOfMsgToRender}
            </div>
            <div className="userType">
              <input
                type="text"
                placeholder="You:"
                label="email"
                value={this.state.msg}
                onChange={this.handleInput}
              />
            <input id="doneButton" type="submit" value="send" onClick={this.handleSend} />
            </div>
          </ div>
        </ div>
      </ div>
    );
  }

In the above code, it is the div dialog, that I would like to have scroll up automatically as it starts to overflow.

I tried to do something like:

componentDidUpdate: function() {
  var node = this.getDOMNode('dialog');
  node.scrollTop = node.scrollHeight;
},

But that wont work

3
  • have you tried this.refs.dialog? since 'dialog' is a div and not a react custom element, you don't need to use this.getDOMNode. this article might help. Commented Apr 11, 2016 at 1:14
  • hi fzxt, you where right this.refs.dialog worked, could you use that as an answer below, and I will accept it. Thanks a bunch Commented Apr 11, 2016 at 1:20
  • Sure thing. I'll write an answer :) Commented Apr 11, 2016 at 5:19

1 Answer 1

1

In order to access a DOM node within a react component, you can do something like this:

say you have a <div ref = "hello">Hello World.</div>

You can access this DOM node in react via: this.refs.hello.

The only thing you have to watch out for is this ONLY works for HTML elements, not React elements. For React elements, you need to use this.getDOMNode(this), and work through it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.