Navigation
==========
RRouter provide several means to navigate between routes of an application. The
most preferred one is to ```` component.
For the example code below we will use the following routing configuration::
Using ```` component
--------------------------
RRouter provides ```` component which renders into ```` DOM component
but handles ``onClick`` by triggering a navigation instead of reloading the
page. First we need to bring ```` component into scope::
var RRouter = require('rrouter')
var Link = RRouter.Link
Setting ``href`` property manually
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The simplest usage of ```` component is analogues to ```` DOM
component, you just specify ``href`` property with desired URL::
About page
This works well but not so convenient when you need to generate a link to a
route with parameters. Instead you can make ``>`` automatically generate
``href`` for you.
Automatically generating ``href`` using route references
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
We can generate a ``href`` to a route named ``main`` with the following code::
Main
.. note::
:class: inline
Route references respect hierarchy of routes. For example to reference a route
with ``User`` view we use ``/users/user`` reference.
Note that instead of passing ``href`` we pass ``to`` property with a reference
to the route with ``Main`` view. The value of ``to`` property is called a route
reference. It's a way to reference a specific route inside routing
configuration.
The advantage of using ```` component with route references is more
visible when we try to generate a link to a route with parameters::
Profile
This will produce a link to ``/users/john``.
Using programmatic navigation
-----------------------------
It is possible to do a programmatic navigation using a routing instance returned
by ``RRouter.start()`` call:
.. note::
:class: inline
Programmatic in a sense of imperativeness as opposed to declarative style
navigation using ```` component.
::
var routing = RRouter.start(routes, function(view) { ... })
The ``routing`` object has ``navigate(path)`` method which can be used to
transition to a different route::
routing.navigate('/users/john')
Note that this requires you to keep a reference to a global ``routing`` object.
This isn't a bad thing per se but for some reason it can be inconvenient.
Another approach to programmatic navigation is to use ``RoutingContextMixin``::
var MyComponent = React.createClass({
mixins: [RoutingContextMixin],
someMethod: function() {
this.navigate('/users/john')
}
})
This mixin makes ``navigate(path)`` method available to a component.