react-scroll icon indicating copy to clipboard operation
react-scroll copied to clipboard

Navigating from another page

Open muscaiu opened this issue 5 years ago • 14 comments

Is it possible to navigate from another page? (using react Router v5 with create-react-app)

For example i have this exmple that is working on the HomePage. But if i click the navbar item from another page it doesn't work.

How i navigate to anchor from another page?

import { Element} from 'react-scroll';

export function HomePage() {
    return (
        <>
                <title>Home</title>
                <Banner />

                <Element name="price-plan-list">
                    <PriceplanList />
                </Element>

                <Element name="entertainment-area">
                    <EntertainmentArea />
                </Element>
        </>
    );
}

And this is the NavbarItem:

import { Link as AnchorLink } from 'react-scroll';

        <NavItem key={index}>
            <AnchorLink
                activeClass="active"
                to={item.link}
                spy={true}
                smooth={'easeInOutCubic'}
                duration={1000}
                offset={-50}
            >
                {item.title}
            </AnchorLink>
        </NavItem>

muscaiu avatar Oct 27 '20 09:10 muscaiu

Have you tried setting the hashSpy={true} on the link?

Otherwise what you could do is on each Page, look for the "elementId" in the url, and then navigate to it programatically.

fisshy avatar Oct 27 '20 11:10 fisshy

hashSpy={true} doesn't work.

How could i navigate programatically? I have the same Navbar for all pages. You mean to switch the AnchorLink for a react-router link?

muscaiu avatar Oct 28 '20 08:10 muscaiu

I tried adding containerId={item.title} as mention in the docs:

hashSpy - update hash based on spy, containerId has to be set to scroll a specific element.

But i'm getting this error and don't know how to fix it:

Uncaught TypeError: Cannot read property 'spyCallbacks' of undefined
    at Object.addSpyHandler

I found this thread related, but still doesn't help: https://github.com/fisshy/react-scroll/issues/352

muscaiu avatar Oct 31 '20 14:10 muscaiu

I made a Codasandbox to ilustrate the issue: https://codesandbox.io/s/scroll-link-example-forked-tk54l?file=/src/Home.js

I want to know if it's possible to navigate and scroll from another page or should i try another library.

Any help is appreciated. Thanks!

muscaiu avatar Oct 31 '20 15:10 muscaiu

Thanks for the codesandbox.

If you want to scroll to the elements on different pages, then you have to render the elements on every page. There's no way for react-scroll to detect elements that doesn't exists.

Easiest would be to wrap the elements in another component and render that component on each page.

fisshy avatar Nov 01 '20 12:11 fisshy

I can't render the HomePage elements on every page.

Would it be possible to make a route switch to '/' before the scrolling happens?

muscaiu avatar Nov 01 '20 17:11 muscaiu

Hi, I've found the package react-scrollable-anchor https://www.npmjs.com/package/react-scrollable-anchor really helpful. This allows to navigate and scroll to a specific section even from another page, just by using href anchor.

astridwalle avatar Dec 01 '20 15:12 astridwalle

@fisshy I have same issue. Do you have any sollutions ?

MartinYounghoonKim avatar Dec 27 '20 14:12 MartinYounghoonKim

@fisshy I have same issue. Do you have any sollutions ?

I do not have any solution to scroll between pages, this was made for SPA aps.

fisshy avatar Dec 27 '20 21:12 fisshy

I ran into the same issue.

AliF50 avatar Dec 29 '20 19:12 AliF50

I do not know if this will help you guys, but what I did (in create-react-app and react-router-dom) is to use a scroller.scrollTo in a setTimeout, example:

<Link 
onClick={()=>{
 setTimeout(()=> {scroller.scrollTo("id", {smooth: true, duration: 15000}}, 750)
}} 
to="/otherPage"
>Go to specific part in other Page</Link> 

I have a page transition hence the 750 milliseconds, but i have tried as low a 1 and it still worked. This solution is kind of jank and I dont know if Search engine bots will like it.

ManWithA1000Names avatar Feb 07 '21 06:02 ManWithA1000Names

People might find https://stackoverflow.com/questions/61779236/how-to-navigate-to-another-page-with-a-smooth-scroll-on-a-specific-id-with-react useful

Stvad avatar Feb 07 '21 21:02 Stvad

I set my page to link to the other page and pass a query string, for instance www.mydomain.com/#querystringhere

Then I evaluate what the hash is that is passed in on page load

const hash = window.location.hash;

  useEffect(() => {
    switch (hash) {
      case "#querystringhere":
        scroller.scrollTo("elementnamehere");
        break;
      default:
        break;
    }
  }, [hash]);

CWSites avatar Jun 28 '22 03:06 CWSites

i have fount this issue as well but i have found a great package https://www.npmjs.com/package/react-router-hash-link it helped ...

adi-4444 avatar May 02 '23 11:05 adi-4444