0

I have a page with long <div>. In this <div> I have scrollbar and button on the bottom. My problem is how to create a function for this button to scroll top inside this div.

Button and div are actually two different components.

In vanilla js, it is pretty easy but in ReactJS I have no idea. Should I try to find the dom node and then scroll to top of it?

I can't use any additional library from npm etc.

Could you please give some advice?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Mizzt
  • 9
  • 1
  • 5

2 Answers2

0

You must use ref for find elements - https://reactjs.org/docs/refs-and-the-dom.html. If you need share your props or state in very diff components you can use context - https://reactjs.org/docs/legacy-context.html#how-to-use-context

You can not use document.querySelector - it`s bad idea for React

Sporx
  • 29
  • 3
0

Have to use ref. You can see this link for the full demo here

GRS
  • 1,829
  • 1
  • 9
  • 23
  • 1
    Answers which point to external sites for code are rarely upvoted and are more likely to be downvoted. If you are the author of the code, and don't mind the license under which it will be placed on Stack Overflow, you can use [Stack Snippets](https://meta.stackoverflow.com/q/358992/215552) to provide the code here. – Heretic Monkey Aug 19 '19 at 14:18