I am currently using react router dom for routing in my react application. I am trying to scroll to specific div on another page using Link from react router dom. The issue I am running into is that the page changes yet it does not scroll to the id of the div i have specified. I am unsure of what I am missing.
my code is as follows: App.js
import { Switch, Route, BrowserRouter as Router } from "react-router-dom";
import Home from "./Home";
import PageTwo from "./PageTwo";
import "./styles.css";
function App() {
return (
<Router>
<div className="App">
<Switch>
<Route exact path="/" ponent={Home} />
<Route path="/pagetwo" ponent={PageTwo} />
</Switch>
</div>
</Router>
);
}
export default App;
Home Page
import { Link } from "react-router-dom";
const Home = () => {
return (
<div>
<div>Navigate to div on new page</div>
<Link to="/pagetwo#div">Home</Link>
</div>
);
};
export default Home;
PageTwo:
const PageTwo = () => {
return (
<div>
<div style={{ marginTop: 500 }} id="div">
Page Two Div
</div>
</div>
);
};
export default PageTwo;
css:
html {
scroll-behavior: smooth;
}
attached is a code sandbox for debugging! =/src/PageTwo.js:0-171
I am currently using react router dom for routing in my react application. I am trying to scroll to specific div on another page using Link from react router dom. The issue I am running into is that the page changes yet it does not scroll to the id of the div i have specified. I am unsure of what I am missing.
my code is as follows: App.js
import { Switch, Route, BrowserRouter as Router } from "react-router-dom";
import Home from "./Home";
import PageTwo from "./PageTwo";
import "./styles.css";
function App() {
return (
<Router>
<div className="App">
<Switch>
<Route exact path="/" ponent={Home} />
<Route path="/pagetwo" ponent={PageTwo} />
</Switch>
</div>
</Router>
);
}
export default App;
Home Page
import { Link } from "react-router-dom";
const Home = () => {
return (
<div>
<div>Navigate to div on new page</div>
<Link to="/pagetwo#div">Home</Link>
</div>
);
};
export default Home;
PageTwo:
const PageTwo = () => {
return (
<div>
<div style={{ marginTop: 500 }} id="div">
Page Two Div
</div>
</div>
);
};
export default PageTwo;
css:
html {
scroll-behavior: smooth;
}
attached is a code sandbox for debugging! https://codesandbox.io/s/silent-hill-0ln79?file=/src/PageTwo.js:0-171
Share Improve this question asked Feb 20, 2021 at 22:07 Rob TerrellRob Terrell 2,5624 gold badges21 silver badges46 bronze badges 3- And where is the code that's suppose to do scrolling? – Nadia Chibrikova Commented Feb 20, 2021 at 22:18
- if I were to type the id of the div I want to scroll to in the url next to the route it would take me to that exact div, for some reason when i use link or navlink etc from react router dom it doesn't work – Rob Terrell Commented Feb 20, 2021 at 22:20
- github./ReactTraining/react-router/issues/… have you tried this? – Nadia Chibrikova Commented Feb 20, 2021 at 22:28
3 Answers
Reset to default 9As of this issue on github use react-router-hash-link
package.
<HashLink to="/pagetwo#div">Home</HashLink>
here is the codesandbox
1. First install
npm i react-router-hash-link
or
npm i react-router-hash-link --force
2. Then import it and Provide the exact path and id of the ponent.
import { HashLink } from 'react-router-hash-link';
<HashLink to="/some/path#with-hash-fragment">Link to Hash Fragment</HashLink>
Note: For smmoth scroll add the following.
<HashLink smooth to="/path#hash">
Link to Hash Fragment
</HashLink>;
Help: react-router-hash-link
Not sure if someone would still e searching for it but we can just use the normal anchor as well instead of installing the above.
<a href="#idToTag'>Tag</a>