Skip to content
This repository was archived by the owner on Oct 26, 2018. It is now read-only.

Render method call twice #197

Closed
oliviertassinari opened this issue Jan 15, 2016 · 16 comments
Closed

Render method call twice #197

oliviertassinari opened this issue Jan 15, 2016 · 16 comments

Comments

@oliviertassinari
Copy link

That may be linked to #193.
I have noticed, when migrating from redux-router to this repository (v2.0.2), that the render method is called twice.
This happens when I'm calling routeActions.push().

  • The first render is triggered on the component I'm transitioning from.
    I think that it should be the component I'm transitioning to.
  • The second render is triggering on the component I'm transitioning to.
    The trigger of this method seems asynchronous (out of the middleware chain). I think that it shouldn't be here.
@timdorr
Copy link
Member

timdorr commented Jan 15, 2016

Route rendering is always an asynchronous process because router has a lot of async handling baked in (route components can be retrieved asynchronously). So the second render you're seeing is expected.

The first, however, is not. Are you connecting your component to some part of state that's updated by the route actions?

@oliviertassinari
Copy link
Author

Route rendering is always an asynchronous

Alright, so I will try to understand why this first render occurres. That could be an issue on my side.

@taion
Copy link
Member

taion commented Jan 16, 2016

I think this is an unavoidable consequence of how we integrate React Router into Redux.

It might be possible to do something very clever with unstable_batchedUpdates, but I'm not entirely sure here.

@timdorr
Copy link
Member

timdorr commented Jan 17, 2016

@taion I'm not so sure. Most people complain about router not re-rendering components when transitioning within the same branch of their route tree.

In any case, this sounds like something not related to redux-simple-router. If something does present itself as specifically our fault, I can reopen this.

@Elijen
Copy link

Elijen commented Mar 27, 2016

@oliviertassinari Are you by any chance using hashHistory instead of browserHistory? I noticed this problem went away after changing to browserHistory.

@oliviertassinari
Copy link
Author

@Elijen I was using it. I have upgraded to the latest release. The issue is gone on my side.

@minhkhanb
Copy link

render function in Component was run twice. Let know me why? Help me? Please!

@GuillaumeMunsch
Copy link

Any news on this ?

@gndplayground
Copy link

<Router history={history} key={Math.random()}>
        <Route path='/' component={AppContainer}>
            <Route path={routesList.login} getComponent={function (path, cb) {
                require.ensure([], (require) => {
                    cb(null, require('./login/Login').default);
                });
            }}/>
            <Route path={routesList.logout} getComponent={function (path, cb) {
                require.ensure([], (require) => {
                    cb(null, require('./login/Login').default);
                });
            }}/>
            <Route onEnter={requireAuth} getComponent={
                function (path, cb) {
                require.ensure([], (require) => {
                    cb(null, require('./dashboard/Dashboard').default);
                });
            }}>
                <Route path={routesList.dashboard} getComponent={function (path, cb) {
                    require.ensure([], (require) => {
                        cb(null, require('./dashboard/pages/Home').default);
                    });
                }}/>
                <Route path={routesList.materialComponent} component={Material}/>
            </Route>
        </Route>
        <Route path='/test' component={Test}>
        </Route>
    </Router>

Supposed I have a router like this. When I go to dashboard, it seems my API called twice. So the dashboard component render twice. I checked with two life cycle below

componentDidMount() {
        console.log('did mount');
    }

    componentDidUpdate() {

        console.log('updated');
    }

My Dashboard component output ('updated');. Hm, so maybe some thing change in next props, next states in life cycle. So I addded two console ouput in shouldComponentUpdate

 shouldComponentUpdate(nextProp, nextState) {
       // Use lodash is Equal
        console.log(_.isEqual(nextProp, this.props));
        console.log(_.isEqual(nextState, this.state));

        return true;

    }

Both state and props is equal to each next state and next props :(. So I change my lifecycle to

shouldComponentUpdate(nextProp, nextState) {
        return !(_.isEqual(nextProp, this.props) && _.isEqual(nextState, this.state));
    }

With this way, my dashboad component won't update if props and states don't change. So my dashboard won't render twice.

In my case it's the solution for me @GuillaumeMunsch

@ivorpad
Copy link

ivorpad commented Nov 22, 2016

@Elijen Thanks to your comment I fixed my bug. hashHistory was causing the render() method to render twice and it was returning a wrong this.props.params object with undefined values from a route.

Do you know why is this by any chance?

@13luck
Copy link

13luck commented Nov 24, 2016

Try to downgrade to react-router@2.8.0

@kdenz
Copy link

kdenz commented Nov 27, 2016

@Elijen Thank you! It solved my problem as well. In my case with react-router 3.0, the render() method rendered 2 extra times. It seems to be a weird bug with hash history.

@hulin32
Copy link

hulin32 commented Jan 11, 2017

Downgrade to react-router@2.8.0, also doesn't work.
Config like this:

"react-redux": "^5.0.2",
"react-router": "2.8.0",
"react-router-redux": "^4.0.7",

zushenyan added a commit to zushenyan/Workstation that referenced this issue Jan 14, 2017
@amangeot
Copy link

Hello,
I had the same issue with multiple route renders. This lead to potential errors with authentication redirections and analytics.

Latest release react-router-redux solved the issue:

"react-redux": "5.0.2",
"react-router": "3.0.2",
"react-router-redux": "4.0.7" ---> "4.0.8",

Thank you,

@shanSyapse
Copy link

This doesn't work for us:

"react-redux": "5.0.2",
"react-router": "3.0.2",
"react-router-redux": "4.0.7" ---> "4.0.8",

We installed the same version and still get the render twice error

@enheit
Copy link

enheit commented Jul 13, 2017

I had the same issue and fix it by upgrading react-router-redux to 4.0.8.

JSON looks like:

"react-redux": "5.0.2",
"react-router: "3.0.5",
"react-router-redux": "4.0.8"

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