Skip to content

Releases: remix-run/react-router

v1.0.0

10 Nov 17:17
Compare
Choose a tag to compare

Thanks for your patience :) Big changes from v0.13.x to 1.0. While on the surface a lot of this just looks like shuffling around API, the entire codebase has been rewritten to handle some really great use cases, like loading routes and components on demand, session-based route matching, server rendering, integration with libs like redux and relay, and lots more.

But for now, here's how to translate the old API to the new one.

Importing

The new Router component is a property of the top-level module.

// v0.13.x
var Router = require('react-router');
var Route = Router.Route;

// v1.0
var ReactRouter = require('react-router');
var Router = ReactRouter.Router;
var Route = ReactRouter.Route;

// or using ES Modules
import { Router, Route } from 'react-router';

Rendering

// v0.13.x
Router.run(routes, (Handler) => {
  render(<Handler/>, el);
})

// v1.0
render(<Router>{routes}</Router>, el)

// looks more like this:
render((
  <Router>
    <Route path="/" component={App}/>
  </Router>
), el);

// or if you'd rather
render(<Router routes={routes}/>, el)

Locations

Locations are now called histories (that emit locations). You import them from the history package, not react router.

// v0.13.x
Router.run(routes, Router.BrowserHistory, (Handler) => {
  render(<Handler/>, el);
})

// v1.0
import createBrowserHistory from 'history/lib/createBrowserHistory'
let history = createBrowserHistory()
render(<Router history={history}>{routes}</Router>, el)

If you do not specify a history type (as in the example above) then you will notice some unusual behavior after updating to 1.0.0. With the default hash based routing a querystring entry not defined by yourself will start appearing in your URLs called _k. An example of how it looks is this: ?_k=umhx1s

This is intended and part of createHashHistory (which is the default history approach used if one is not specified). You can read more about the feature here and how to opt out here.

Route Config

You can still nest your routes as before, paths are inherited from parents just like before but prop names have changed.

// v0.13.x
<Route name="about" handler={About}/>

// v1.0
<Route path="about" component={About}/>

Named routes are gone (for now, see discussion)

NotFound route

Not found really confused people, mistaking not finding resources from your API for not matching a route. We've removed it completely since it's simple with a * path.

// v0.13.x
<NotFoundRoute handler={NoMatch}/>

// v1.0
<Route path="*" component={NoMatch}/>

Redirect route

// v0.13.x
<Redirect from="some/where/:id" to="somewhere/else/:id" params={{id: 2}}/>

// v1.0
// Works the same as before, except no params, just put them in the path
<Redirect from="/some/where/:id" to="/somewhere/else/2"/>

Links

path / params

// v0.13.x
<Link to="user" params={{userId: user.id}}>Mateusz</Link>

// v1.0
// because named routes are gone, link to full paths, you no longer need
// to know the names of the parameters, and string templates are quite
// nice. Note that `query` has not changed.
<Link to={`/users/${user.id}`}>Mateusz</Link>

"active" class

In 0.13.x links added the "active" class by default which you could override with activeClassName, or provide activeStyles. It's usually just a handful of navigation links that need this behavior.

Links no longer add the "active" class by default (its expensive and usually not necessary), you opt-in by providing one; if no activeClassName or activeStyles are provided, the link will not check if it's active.

// v0.13.x
<Link to="about">About</Link>

// v1.0
<Link to="/about" activeClassName="active">About</Link>

Linking to Index routes

Because named routes are gone, a link to / with an index route at / will always be active. So we've introduced IndexLink that is only active when on exactly that path.

// v0.13.x
// with this route config
<Route path="/" handler={App}>
  <DefaultRoute name="home" handler={Home}/>
  <Route name="about" handler={About}/>
</Route>

// will be active only when home is active, not when about is active
<Link to="home">Home</Link>

// v1.0
<Route path="/" component={App}>
  <IndexRoute component={Home}/>
  <Route path="about" component={About}/>
</Route>

// will be active only when home is active, not when about is active
<IndexLink to="/">Home</IndexLink>

This gives you more granular control of what causes a link to be active or not when there is an index route involved.

onClick handler

For consistency with React v0.14, returning false from a Link's onClick handler no longer prevents the transition. To prevent the transition, call e.preventDefault() instead.

RouteHandler

RouteHandler is gone. Router now automatically populates this.props.children of your components based on the active route.

// v0.13.x
<RouteHandler/>
<RouteHandler someExtraProp={something}/>

// v1.0
{this.props.children}
{React.cloneElement(this.props.children, {someExtraProp: something})}

Note: React does not validate propTypes that are specified via cloneElement (see: facebook/react#4494). It is recommended to make such propTypes optional.

Navigation Mixin

If you were using the Navigation mixin, use the History mixin instead.

// v0.13.x
var Assignment = React.createClass({
  mixins: [ Navigation ],
  navigateAfterSomethingHappened () {
    this.transitionTo('/users', { userId: user.id }, query);
    // this.replaceWith('/users', { userId: user.id }, query);
  }
})

// v1.0
var Assignment = React.createClass({
  mixins: [ History ],
  navigateAfterSomethingHappened () {
    // the router is now built on rackt/history, and it is a first class
    // API in the router for navigating
    this.history.pushState(null, `/users/${user.id}`, query);
    // this.history.replaceState(null, `/users/${user.id}`, query);
  }
})

The following Navigation methods are now also found on the history object, main difference again is there are no params or route names, just pathnames.

v0.13 v1.0
go(n) go(n)
goBack() goBack()
goForward() goForward()
makeHref(routeName, params, query) createHref(pathname, query)
makePath(routeName, params, query) createPath(pathname, query)

State mixin

// v0.13.x
var Assignment = React.createClass({
  mixins: [ State ],
  foo () {
    this.getPath()
    this.getParams()
    // etc...
  }
})

// v1.0
// if you are a route component...
<Route component={Assignment} />

var Assignment = React.createClass({
  foo () {
    this.props.location // contains path information
    this.props.params // contains params
    this.props.history.isActive
  }
})

// if you're not a route component, you need to pass location down the
// tree or get the location from context. We will probably provide a
// higher order component that will do this for you but haven't yet.
// see further down for more information on what can be passed down
// via context
var Assignment = React.createClass({
  contextTypes: {
    location: React.PropTypes.object
  },
  foo () {
    this.context.location
  }
})

Here's a table of where you used to get stuff with the State mixin, and where you get it now if you're a route component (this.props)

v0.13 (this) v1.0 (this.props)
getPath() location.pathname+location.search
getPathname() location.pathname
getParams() params
getQuery() location.search
getQueryParams() location.query
getRoutes() routes
isActive(to, params, query) history.isActive(pathname, query, onlyActiveOnIndex)

Here is another table of properties you used to get via the State and where you can get it now if you are not a route component (this.context).

v0.13 (this) v1.0 (this.context)
getPath() location.pathname+location.search
getPathname() location.pathname
getQuery() location.search
getQueryParams() location.query
isActive(to, params, query) history.isActive(pathname, query, indexOnly)

Note not all State functionality can be accessed via context in v1.0. For example, params is not available via context.

Scrolling

We're developing scroll behaviors separately in the scroll-behavior library until we have a stable, robust implementation that we're happy with. Currently, scroll behaviors are exposed there as history enhancers:

import createHistory from 'history/lib/createBrowserHistory'
import useScroll from 'scroll-behavior/lib/useStandardScroll'

const history = useScroll(createHistory)()

willTransitionTo and willTransitionFrom

Routes now define this behavior:

// v0.13.x
var Home = React.createClass({
  statics: {
    willTransitionTo (transition, params, query, callback) { }
    willTransitionFrom (component, transition, params, query, callback) { }
  }
})

// v1.0
<Route
  component={Home}
  onEnter={(location, replaceWith) => {}}
  onLeave={() => {}}
/>

To cancel a "transition from", please refer to the Confirming Navigation guide.

v1.0.0-rc4

10 Nov 17:26
Compare
Choose a tag to compare
v1.0.0-rc4 Pre-release
Pre-release
  • ec3f3eb [wip][added] Support custom RoutingContext on Router
  • 4b8e994 [fixed] Index routes with extraneous slashes
  • c68f9f1 [removed] Remove deprecated handler prop on Route
  • 9e449f5 [fixed] Match routes piece-by-piece
  • fbc109c [fixed] Use %20 instead of + in URL pathnames
  • 8dd8ceb [fixed] Mark dynamic index routes as active
  • fae04bc [added] Greedy splat (**)
  • 24ad58c [changed] Query changes aren't route changes
  • 19c7086 [added] Handle undefined query values in isActive
  • 3545ab2 [removed] params from RoutingContext child context
  • 9d346fc [added] params on RoutingContext child context
  • 52cca98 [changed] Preventing transition from onClick
  • 4e48b6b [changed] Pass named children as props
  • 3cc7d6d [added] Peer dependency on history package
  • 751ca25 [fixed] Don't match empty routes for isActive
  • 8459755 [fixed] Include the hash prop on Links
  • ac7dd4a [fixed] Postinstall script on Windows
  • d905382 [added] Test for IndexLink to deeply nested IndexRoute
  • 6f8ceac [fixed] Transitions example

v0.13.5

10 Nov 17:16
Compare
Choose a tag to compare
v0.13.5 Pre-release
Pre-release
  • #2262 [added] Support for React v0.14

v1.0.0-rc3

10 Nov 17:26
Compare
Choose a tag to compare
v1.0.0-rc3 Pre-release
Pre-release
  • e42a4ad [changed] Stop using npm prepublish script
  • 3be6a2d [fixed] Workaround for nasty npm bug

v1.0.0-rc2

10 Nov 17:25
Compare
Choose a tag to compare
v1.0.0-rc2 Pre-release
Pre-release
  • bdab3d8 [fixed] Compare query by string value
  • c43fb61 [added] prop
  • 24e7b4f [fixed] isActive on nested IndexLink
  • 160c5ba [fixed] Removed warning about no history in context
  • ca9e3b7 [added] IndexRedirect
  • 428da54 [added] Support
  • ebb8d20 [fixed] Remove direct calls to createLocation.
  • fc8a7a4 [changed] Run examples using HTML5 history
  • 37d9bac [fixed] isActive on
  • be37196 [fixed] Actually update state when there are transition hooks
  • b8f1abe [changed] Removed (un)registerRouteHook
  • 69a9240 [fixed] Added missing IndexLink to exports

v0.13.4

10 Nov 17:16
Compare
Choose a tag to compare
v0.13.4 Pre-release
Pre-release
  • b237238 [fixed] Removed getter for IE8 compat
  • 53ce7f1 [fixed] #1148 regex for detecting trailing slashes

v1.0.0-rc1

10 Nov 17:24
Compare
Choose a tag to compare
v1.0.0-rc1 Pre-release
Pre-release
  • 5fbe933 [changed] Do not add "active" class by default
  • 85c699c [changed] State -> IsActive

v1.0.0-beta4

10 Nov 17:23
Compare
Choose a tag to compare
v1.0.0-beta4 Pre-release
Pre-release
  • 94509e7 [added] IndexLink
  • adc0a2f [added] IndexRoute
  • b86509a [added] useRoutes history enhancer [added] RoutingContext component [added] RouteContext mixin [added] Lifecycle mixin
  • e72812d [added]
  • 4c6dc1b [fixed] Installing on Windows
  • 042cffc [changed] Removed histories/added history dep
  • af7eb55 [added] History.onBeforeChange
  • f4ed900 [fixed] correctly updates the window scroll position
  • 587e54f [added] static history singleton getter for HashHistory and BrowserHistory
  • 5bd62b5 [fixed] errors in examples
  • 4e2ca3c [fixed] URI escape path components with special chars
  • 0630488 [fixed] Link module adds extra space
  • 26400c1 [fixed] Use encodeURI for splat params
  • 178efc3 [fixed] when using HashHistory
  • 41bd525 [fixed] Properly escape splats
  • 4759961 [fixed] URLUtils recognize values containing \n
  • 2389c61 [changed] Export history classes
  • 824ed63 [fixed] handling
  • 2447ecb [added] formatPattern util method

v1.0.0-beta3

10 Nov 17:23
Compare
Choose a tag to compare
v1.0.0-beta3 Pre-release
Pre-release

v1.0.0-beta2

10 Nov 17:22
Compare
Choose a tag to compare
v1.0.0-beta2 Pre-release
Pre-release