Answer the question
In order to leave comments, you need to log in
How to make Link from react-router scroll to div?
There is a regular menu with a couple of links. Some of them lead to separate pages, and the rest should scroll to the desired div on the page.
Menu items are rendered like this:
renderNav() {
return menu.map((item, index) => {
return (
<li className='menu-item' key={index}>
<Link to={`${item.link}`} className="menu-link" activeClassName="menu-link--is_active">{item.name}</Link>
</li>
)
})
}
Answer the question
In order to leave comments, you need to log in
1. If you have complex logic: Link accepts the state property , through it you can pass an identifier where to scroll, for example.
Then subscribe to history :
let unlisten = history.listen(location => {
console.log(location.state); // <-- переданный в Link state.
})
<Link to="some" hash="#idOfElement" >Scroll to</Link>
let unlisten = history.listen(location => {
console.log(location.hash); // <-- переданный в Link hash.
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question