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

Properly initialize <Router> state after match #2935

Closed
morlay opened this issue Jan 19, 2016 · 1 comment
Closed

Properly initialize <Router> state after match #2935

morlay opened this issue Jan 19, 2016 · 1 comment
Labels
Milestone

Comments

@morlay
Copy link

morlay commented Jan 19, 2016

Since could through the renderProps to Router
https://github.com/rackt/react-router/blob/v2.0.0-rc5/modules/Router.js#L46
we could use match on client like this ? (should this correct ? )

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

class App extends React.Component {
  render() {
    console.log(this.context.router.isActive('/about')); // false on first render
    return (
      <div>
        <h1>App</h1>
        {this.props.children}
      </div>
    )
  }
}

App.contextTypes = {
  router: React.PropTypes.object
};

const routes = {
  path: '/',
  component: App,
  childRoutes: [
    {
      path: '/about',
      component: App,
      onEnter: (nextState, replace, callback) => {
        setTimeout(() => {
          callback()
        }, 1000)
      }
    }
  ]
};

const { pathname, search, hash } = window.location;
const location = `${pathname}${search}${hash}`;

match({
  routes,
  location,
  history
}, (error, redirectLocation, renderProps) => {
  console.log(renderProps.router.isActive('/about')); // return true
  render(
    <Router
      {...renderProps}
      history={history} // if pass renderProps.history, well show `router.history` warning.
    />,
    document.getElementById('app')
  )
});

However, �
If an async method in onEnter,
The router in Router will re-initial, and the router.isActive will return false, until onEnter callback, and re-render the Router. ( see the console.log in code, above)

This may break lazy match in client.
And in server render, The Link active state, will be different between client and server.

@taion taion added the bug label Jan 19, 2016
@taion taion added this to the v2.0.0-final milestone Jan 19, 2016
@taion taion changed the title If an async method in onEnter,context.router.isActive is not same as renderProps.router.isActivewhen first render. Properly initialize <Router> state after match Jan 27, 2016
@taion
Copy link
Contributor

taion commented Jan 27, 2016

Let's track this on #2965 – the fix is a bit broader than what's described here.

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

No branches or pull requests

2 participants