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

Using match() on client causes onEnter() to be called twice on initial page load. #3016

Closed
tybro0103 opened this issue Feb 2, 2016 · 7 comments

Comments

@tybro0103
Copy link

This only happens on the initial page load if you go directly a route (/planets in this case) with an onEnter(). Subsequent visit to that route via pushState will only trigger it once.

I believe this is happening because match() and then render(<Router> ...) are both triggering the hook. Perhaps there's a way to avoid this?

This can be reproduced with minimal setup:

client/index.js

import React from 'react';
import { render } from 'react-dom';
import { Router, browserHistory, match } from 'react-router';

import routes from '../app/routes';

let {pathname, search, hash} = window.location;
let location = `${pathname}${search}${hash}`;
let appMainEl = document.getElementById('app-main');

match({routes, location}, (error, redirect, renderProps) => {
  var component = <Router {...renderProps} history={browserHistory} />;
  render(component, appMainEl);
});

app/routes.js

import React from 'react';
import { Route, IndexRoute } from 'react-router';

import App from './components/app';
import Home from './components/home';
import Planets from './components/planets/';

export default (
  <Route path="/" component={App}>
    <IndexRoute component={Home}/>
    <Route path="/planets" component={Planets} onEnter={onPlanetsEnter} />
  </Route>
);

function onPlanetsEnter() {
  // happens twice!
  console.log('PLANETS ENTER!');
}
@tybro0103 tybro0103 changed the title Using match() on client causes onEnter() to be called twice on initial page load. Using match() on client causes onEnter() to be called twice on initial page load. Feb 2, 2016
@taion
Copy link
Contributor

taion commented Feb 2, 2016

It's been fixed on master.

@taion taion closed this as completed Feb 2, 2016
@tybro0103
Copy link
Author

@taion oh good. Do you know which commit? And perhaps when it would make it into an RC?

@taion
Copy link
Contributor

taion commented Feb 2, 2016

#2965

Need a new history RC first to resolve some other bugs.

@stephenjwatkins
Copy link

@taion did this ever make it into a published react-router version?

I'm on [email protected] and I'm still getting onEnter hooks being called twice on initial page load when using match().

@taion
Copy link
Contributor

taion commented Jul 15, 2016

This made it in. Check the docs – the API is a bit different.

@JackyXiong
Copy link

@stephenjwatkins
I found when I use <a>, the onEnter will be called twice, but use Link from react-router will not. Why this happens? I'm really confused.

@jonashartwig
Copy link

jonashartwig commented Dec 15, 2016

Hi, I use react-router and history ^3.0.0 and i get this behavior as well but only on the main route.

        <Router history={history}>
                    <Redirect from="/" to={currentLocale()} />
                    <Route path="/:locale" onEnter={() => console.log("called twice")} component={HomeView} />
        </Router>

regards

@lock lock bot locked as resolved and limited conversation to collaborators Jan 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants