Menu

How to Scroll Between Separate Components in React
📰

How to Scroll Between Separate Components in React

DEV Community: react·Amelia Dutta·3 days ago
#tehsbm9j
#devtoreact#devto#dev#component#button
Reading 0:00
15s threshold

Let's say you're creating a reusable button component in React and want to use it to navigate to a particular section or component of your website from various other components.

One approach we're familiar with is using <Link /> and <Route /> . However, these are primarily used for navigation between different routes. What if you want to scroll to a specific section within the same page instead?

That's where this blog comes in! You'll learn how to scroll between separate components in React.

First, let's create a Button component.

export default function Button ( ) { return ( < button > Scroll to View </ button > ) ; }

Enter fullscreen mode

Exit fullscreen mode

Next, let's create the section/component we want to scroll to.

export default function CatGallery ( ) { return ( < div > < h2 > Hey there , Hooman !</ h2 > < img src = "https://images.unsplash.com/photo-1592194996308-7b43878e84a6?q=80&w=687&auto=format&fit=crop&ixlib=rb-4.1.0&ixid=NHwxMjA3fDA%3D" alt = "orange cat" /> </ div > ) ; }

Enter…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.