Router: Using 'Link' tag's property 'to' to go to hash like '#1' can't trigger hashchange event

Created on 22 Jan 2019  路  2Comments  路  Source: reach/router

I 'm Using Link tag's property to to go to hash like #1, #2,

<Link to='#1'>1</Link>
<Link to='#2'>2</Link>

When I click the link, the hash does change,but it can't trigger hashchange event.

componentDidMount() {
        window.addEventListener(
            'hashchange',
            a => {
                console.log('hashchange', a);
            },
            false,
        );
    }

So I'm wondering how can I do something when hash changes like this?
Thanks :)

question

Most helpful comment

I have the same problem, but according to the source of the Link component, it allows onClick functions. (https://github.com/reach/router/blob/master/src/index.js#L392)

In my case, I could solve this using such an onClick function

All 2 comments

I have the same problem, but according to the source of the Link component, it allows onClick functions. (https://github.com/reach/router/blob/master/src/index.js#L392)

In my case, I could solve this using such an onClick function

Agreed that the best we'd get would be to use the onClick. I've seen a few other projects take this approach and is probably the best we'll get since reach doesn't have any plans to support hashrouting

Was this page helpful?
0 / 5 - 0 ratings