React-router: Pass the router, or state, on Router's onUpdate method

Created on 14 Jul 2015  路  5Comments  路  Source: ReactTraining/react-router

It will be great if we get some info about the router, and state, when the onUpdate callback is raised. Also, it is impossible to use the addTransitionHook, before, or during, the React.render(

Most helpful comment

You can get info about router state from this.
Something like:

        <Router
          history={history}
          routes={routes}

          onUpdate={function() {
            store.dispatch(routerStateChange(this.state));
          }}
        />

All 5 comments

+1

I'm trying to implement a router store for v1.0.0-beta3 so I can replicate fluxible-router's behavior (so it's just a HOC, store, and an action), but it's proven to be tricky.

You can get info about router state from this.
Something like:

        <Router
          history={history}
          routes={routes}

          onUpdate={function() {
            store.dispatch(routerStateChange(this.state));
          }}
        />

@faergeek That works, thank you!

What's the reason for not passing on information through arguments?
Now it is potentially an issue whenever someone (like me, currently) would like to have access to both the scope of the component as the scope of the onUpdate handler.

export default class App extends React.Component {

  handleUpdate () {
    // now it's impossible to access the `this` scope of the `onUpdate`-handler, 
    // which means no reference to the state of the router
  }  

  render () {
    return (
      <Provider store={store}>
        {() => <Router
                 history={history}
                 children={routes}
                 // bind the handler to another scope
                 onUpdate={this.handleUpdate.bind(this)} 
               />
        }
      </Provider>
    )
  }
}

Good question.

/cc @mjackson @ryanflorence

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Waquo picture Waquo  路  3Comments

ackvf picture ackvf  路  3Comments

winkler1 picture winkler1  路  3Comments

Radivarig picture Radivarig  路  3Comments

imWildCat picture imWildCat  路  3Comments