Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll page to the nested React component on a button click

I've got a React Component with several nested components. For page navigation I have a few buttons on the top. The question is, how to scroll the page to the nested react component when a certain button is clicked, is it possible to do so without using jquery libs?

  render() {
    return (
      <div>
        <button onClick={(e) => { console.log('Scrolling to Item 1');}}>Button0</button>
        <button onClick={(e) => { console.log('Scrolling to Item 2');}}>Button1</button>
          <Layout>
            <Item>
              <Content>
            ...
              </Content>
            </Item>

            <Item>
              <Content>
            ...
              </Content>
            </Item>

          </Layout>
        }
      </div>
    );
  }
like image 288
Eugene Avatar asked Jan 17 '17 08:01

Eugene


People also ask

How to scroll to an element on click in react JS?

Scroll to an element on click in React js. 2. Now open terminal inside your react js project and install react-scroll. 3. Go to your src folder and create a components folder. 4. Now inside your components folder create your first component with the name header.js. 5. Open the header.js file in your ...

What is react-scroll-to-top?

The react-scroll-to-top library is a lightweight, customizable button component, that scrolls to the top of the page when clicked. This component is analogous to our own, but you don't have to code it up yourself. Naturally, it only appears once you've scrolled down enough that it makes sense for it to appear.

How to scroll to the specific element/ component of a component?

Put an id attribute ('myTarget') to your target component and replace the button with a link (href: '#mytarget'). This does not work with fixed headers, unfortunately. Use scrollIntoView method to scroll to the specific Element/ Component If you are using typescript.

How to use useref react hooks?

Basic Knowledge of useRef React hooks. Basic Setup: You will start a new project using create-react-app so open your terminal and type. Now go to your react-scroll folder by typing the given command in the terminal. Required module: Install the dependencies required in this project by typing the given command in the terminal.


3 Answers

Have you tried using an anchor?

Put an id attribute ('myTarget') to your target component and replace the button with a link (href: '#mytarget').

This does not work with fixed headers, unfortunately.

like image 54
pintxo Avatar answered Nov 02 '22 18:11

pintxo


Using React hooks and no 3rd party packages:

  1. Create a reference using the React hook useRef
import { useRef } from 'react'
const firstItemRef = useRef(null);
  1. Assign the reference to the component you want to scroll to
<Item ref={firstItemRef}>
  1. Use the scrollIntoView method to scroll to the specific Element/Component
<button onClick={() => firstItemRef.current.scrollIntoView()}

like image 25
Darren G Avatar answered Nov 02 '22 19:11

Darren G


  1. Create a reference using useRef

    const trackButton = useRef();
    
  2. Use scrollIntoView method to scroll to the specific Element/ Component

        trackButton.current.scrollIntoView({ behavior: "smooth", block: "start", inline: "nearest" })
    
like image 37
Siva Avatar answered Nov 02 '22 17:11

Siva