Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Match fails on the first render when using hashHistory #415

Open
sebasjm opened this issue Jan 19, 2022 · 1 comment
Open

Match fails on the first render when using hashHistory #415

sebasjm opened this issue Jan 19, 2022 · 1 comment

Comments

@sebasjm
Copy link

sebasjm commented Jan 19, 2022

Using <Match /> before <Route history={createHashHistory()} /> will try to match non-hash history so the <Router /> component must be rendered before using match.

Also <Link /> activeClassName is affected

Repository https://github.com/sebasjm/preact-router-match-history-issue
To reproduce, go to profile and refresh the page. The upper header will take the path as / but the lower header will take the correct one.

Using preact-router@3.2.1

Maybe exporting an init function to set this customHistory before rendering?
https://github.com/preactjs/preact-router/blob/main/src/index.js#L14

import Router from 'preact-router';
import Match from 'preact-router/match';

// this will fail
// when rendering https://host/path#/subpath it will take /path
render(
  <div>
    <Match>{({ path }) => <pre>{path}</pre>}</Match>
    <Router history={createHashHistory()}>
      <div default>demo fallback route</div>
    </Router>
  </div>
);

// this will match correctly
// when rendering https://host/path#/subpath it will take /subpath
const customHistory = createHashHistory()
render(
  <div>
    <Router history={customHistory} />
    <Match>{({ path }) => <pre>{path}</pre>}</Match>
    <Router history={customHistory}>
      <div default>demo fallback route</div>
    </Router>
  </div>
);
@enricozb
Copy link

Indeed still a bug with v4.1.2, the fix still works as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants