I have a "Header" component and a separate "Main Page", which displays the rest of the components, such as "AboutUs", including the "Header" itself. How to make it so that when you click on the navbar item in the Header component, the Main component scrolls the page to, for example, the "About Us" container.
Thank you.
scroll to a component from clicking navbar button in another component
2
Answers
I would advise using react-scroll. I’ve used it myself many times.
Wrap the component you want to scroll to with Element component and give it a unique name as the name prop:
Create a Link component wherever you want to trigger the scroll to the myComponent. Pass the to prop with the same name value of the Element component:
You can check for customization options of the Link component on their page
You can use an anchor to "focus" the div
and then use the following javascript:
or you can do it below: