# Auto scroll to bottom of React div

**URL:** <https://forum.freecodecamp.org/t/auto-scroll-to-bottom-of-react-div/450780>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [March 12, 2021, 9:53am UTC](https://forum.freecodecamp.org/t/auto-scroll-to-bottom-of-react-div/450780 "2021-03-12T09:53:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjbatty](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mjbatty/32/202896_2.png) [@mjbatty](https://forum.freecodecamp.org/u/mjbatty)\
**Post date:** [March 12, 2021, 9:53am UTC](https://forum.freecodecamp.org/t/auto-scroll-to-bottom-of-react-div/450780/1 "2021-03-12T09:53:03Z")

</div>

I use chrome dev tools when needed but also like old school `printf` debug so I’m working on a template with a Debug component (still in progress).

I add debug messages to an array and render it by writing each element to a `<p>` tag (in `writeBuffer`) which is wrapped in a div with class name `writebuffer`; the css has `overflow-y: scroll` so I can scroll back and forth as needed. This also means I can copy all messages from the Debug window; I also implemented a simple print as well.

The problem I am trying to solve is that I want the div to auto scroll to the bottom on each render, to get an effect like `tail -f` so I always see the last output. Note I also want to keep the debug controls (buttons) on the left.

Can anyone help please?

Oops - forget to link the pen: [Debug template](https://codepen.io/mjbatty/pen/bGBOxPq)

---

<div class="post-metadata">

**Author:** ![Sky020](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sky020/32/128093_2.png) [@Sky020](https://forum.freecodecamp.org/u/Sky020)\
**Post date:** [March 12, 2021, 11:47am UTC](https://forum.freecodecamp.org/t/auto-scroll-to-bottom-of-react-div/450780/2 "2021-03-12T11:47:58Z")

</div>

Welcome, there.

It should be as easy as using the `.scrollTop` property: [Element.scrollTop - Web APIs | MDN (mozilla.org)](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTop)

It is quite common.

I would try something like this:

```auto
writeBuffer() {
    try {
    const buffer = document.getElementById("printBuffer");
    buffer.scrollTop = buffer.scrollHeight;
    } catch (err) {
      console.log(err);
    }
    return (
      <div>
        {this.state.debugMsgs.map((m, i) => (
          <p key={i}>{m}</p>
        ))}
      </div>
    );
  }

```

The thing is, the height is not exact. So, you will need to tweak around the values.

Also, it is not ideal that it is within a `try...catch` like that. I just did that for ease of testing, but you can change it.

A better idea would be to find a way to get the `.scrollTop` to be set after returning.

Hope this helps

---

<div class="post-metadata">

**Author:** ![mjbatty](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mjbatty/32/202896_2.png) [@mjbatty](https://forum.freecodecamp.org/u/mjbatty)\
**Post date:** [March 12, 2021, 4:07pm UTC](https://forum.freecodecamp.org/t/auto-scroll-to-bottom-of-react-div/450780/3 "2021-03-12T16:07:21Z")

</div>

@Sky020 , thanks very much for that - it got me in the right direction.

The solution I came up with is good enough to handle ~50,000 lines without much performance hit - good enough for me.

I updated the pen (but still in progress): [Debug template](https://codepen.io/mjbatty/full/bGBOxPq).

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [September 11, 2021, 4:07am UTC](https://forum.freecodecamp.org/t/auto-scroll-to-bottom-of-react-div/450780/4 "2021-09-11T04:07:46Z")

</div>

Closed on September 11, 2021, 4:07am UTC

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
