You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

安装react-scroll后导航栏点击无法实现平滑滚动问题排查求助

React Scroll: LinkScroll Not Triggering Smooth Scroll On Navbar Click

I'm having an issue with react-scroll—I want clicking navbar links to smoothly scroll to corresponding sections on the page, but nothing happens when I click the nav items. I've already run npm install react-scroll, here's my relevant code:

App.js

import './App.css';
import React from 'react';
import Navbar from './components/Navbar/Navbar';
import About from './components/About/About';
import Skills from './components/Skills/Skills';
function App() {
  return (
    <React.Fragment>
      <header>
        <Navbar />
      </header>
      <main>
        <About id='about' />
        <Skills id='skills' />
      </main>
    </React.Fragment>
  );
}
export default App;
import { Link as LinkScroll } from 'react-scroll';
const Navbar = () => {
  return (
    <nav>
      <LinkScroll activeClass='active' to='about' spy={true} smooth={true} offset={-70} duration={500} >
        About
      </LinkScroll>
      <LinkScroll activeClass='active' to='skills' spy={true} smooth={true} offset={-70} duration={500} >
        Skills
      </LinkScroll>
    </nav>
  );
};
export default Navbar;

Let's break down the most likely reasons your smooth scroll isn't working, and how to fix them:

1. Target Element ID Isn't Properly Attached

First, double-check that your About and Skills components actually render a DOM element with the passed id prop. For example, if your About component looks like this:

// Wrong - id isn't bound to the root DOM element
function About(props) {
  return (
    <div>
      <h1>About Me</h1>
      {/* ...content */}
    </div>
  );
}

React Scroll won't be able to find the target. Fix it by passing the id to the root element of the component:

// Correct
function About(props) {
  return (
    <div id={props.id}>
      <h1>About Me</h1>
      {/* ...content */}
    </div>
  );
}

2. Missing ScrollProvider (For Newer react-scroll Versions)

If you're using react-scroll v3.x or higher, the library requires wrapping your app with a ScrollProvider to manage scroll context properly. Update your App.js like this:

import './App.css';
import React from 'react';
import { ScrollProvider } from 'react-scroll'; // Add this import
import Navbar from './components/Navbar/Navbar';
import About from './components/About/About';
import Skills from './components/Skills/Skills';

function App() {
  return (
    <ScrollProvider> {/* Wrap your app here */}
      <React.Fragment>
        <header>
          <Navbar />
        </header>
        <main>
          <About id='about' />
          <Skills id='skills' />
        </main>
      </React.Fragment>
    </ScrollProvider>
  );
}
export default App;

3. Simplify Your LinkScroll Configuration

Sometimes extra props can cause conflicts, especially if there's a version mismatch. Try stripping down the component to only essential props first to test if scroll works:

<LinkScroll to='about' smooth={true} duration={500}>
  About
</LinkScroll>

If this works, gradually add back activeClass, spy, and offset to isolate which prop might be causing issues.

4. Check Browser Console for Errors

Open your browser's DevTools (F12) and check the Console tab—you might see warnings like "Target element not found" which will directly point you to the problem. Common issues here include typos in the to prop value, or the target element being unmounted when the link is clicked.

5. Verify Navbar Positioning (Offset Value)

While this won't prevent scrolling entirely, if your navbar is fixed to the top, an incorrect offset value might make it look like nothing happened (the scroll happens but the content is hidden under the navbar). Test removing the offset prop temporarily to rule this out.

内容的提问来源于stack exchange,提问作者user16589580

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 11:27:37