I want to use React to Scroll to Specific Section of my Website, but the navbar, pages all are different .js files ChatGPT To implement scrolling to specific sections of your website using React, even when the components are in different files, you can use the react-scroll library. This library provides a smooth scrolling experience between different sections of your webpage. Here are the steps to achieve this: npm install react-scroll // Navbar.js import React from 'react'; import { Link, animateScroll as scroll } from 'react-scroll'; const Navbar = () => { return ( <nav> <ul> <li> <Link activeClass="active" to="home" spy={true} offset={-70} duration={1500} smooth={'easeOutElastic'} > Home </Link> </li> <li> <Link activeClass="active" to="about" spy={true} offset={-70} duration={1500} smooth={'easeOutElastic'} > About </Link> </li> {/* Add more links for other sections */} </ul> </nav> ); }; export default Navbar; // Home.js import React from 'react'; const Home = () => { return ( <div id="home"> <h1>Home Section</h1> {/* Add content for the home section */} </div> ); }; export default Home; // App.js import React from 'react'; import Navbar from './Navbar'; import Home from './Home'; import About from './About'; // Import other section components const App = () => { return ( <div> <Navbar /> <Home /> <About /> {/* Render other section components */} </div> ); }; export default App; Now, when you click on the navigation links in the Navbar, the page will smoothly scroll to the corresponding sections. Make sure to customize the content of each section and adjust the Link components in the Navbar accordingly.
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter