Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jump to a specific section in a page -React

I have a footer components with some links which each one of them leads to a specific part of a page, how can I make the link jump to this specific part instead of routing only to that page.

<li><Link to="/about-us/#section1">Our History</Link></li> <li><Link to="/about-us/#section2">Our Mission</Link></li> <li><Link to="/about-us/#section3">Our Values</Link></li> <li><Link to="/about-us/#section4">Become a Distributor</Link></li>

I have tried the previous code using id of the same tag in the target section, but it didn't work. Is there any other way to do that.

Thanks,

like image 541
L. Dabbeet Avatar asked Nov 02 '25 04:11

L. Dabbeet


1 Answers

Since you are using React Router, it does not have built in hash link support

You can use this library to accomplish instead:

https://www.npmjs.com/package/react-router-hash-link

import { HashLink } from 'react-router-hash-link';
<HashLink smooth to='/about-us/#section1' > History </HashLink>

your section will need to have the id to match as well

<div id='section1'>Hi</div>
like image 106
Willman.Codes Avatar answered Nov 03 '25 20:11

Willman.Codes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!