HyperRouter

HyperRouter is a DSL wrapper for ReactRouter v4.x to provide client-side routing for Single Page Applications (SPA). As the user changes "pages" instead of reloading from the server your App will mount different components.

This Page Under Construction

Usage

class AppRouter
  include Hyperstack::Component
  include Hyperstack::Router::Helpers
  include Hyperstack::Router

  render(DIV) do
    UL do
      LI { Link('/') { 'Home' } }
      LI { Link('/about') { 'About' } }
    end
    Route('/', exact: true, mounts: Home)
    Route('/about', mounts: About)
  end
end

class Home
  include Hyperstack::Component
  render(DIV) do
    H2 { 'Home' }
  end
end

DSL

Router

This is the Router module which you include in your top level component:

With the base Router class, you can also specify the history you want to use.

This can be done either using a macro:

The macro accepts three options: :browser, :hash, or :memory.

Or by defining the history method:

Rendering a Router

Use the render macro as normal. Note you cannot redefine the render instance method in a Router componenent

Routes

Routes are defined with special pseudo components you call inside the router/components. The router determines which of the routes to actually mount based on the current URL.

The Route method takes a url path, and these options:

  • mounts: Component The component you want to mount when routed to

  • exact: Boolean When true, the path must match the location exactly

  • strict: Boolean When true, the path will only match if the location and path both have/don't have a trailing slash

The Route method can also take a block instead of the mounts option.

The block will be given the match, location, and history data:

  • The Hyperstack::Router::Helpers is useful for components mounted by the router.

  • This automatically sets the match, location, and history params,

    and also gives you instance methods with those names.

  • You can use either params.match or just match.

    and gives you access to the Route method and more.

  • This allows you to create inner routes as you need them.

Normally routes will always render alongside sibling routes that match as well.

Switch

Going to /goodbye would match /:name as well and render Greet with the name param with the value 'goodbye'. To avoid this behavior and only render one matching route at a time, use a Switch component.

Now, going to /goodbye would match the Goodbye route first and only render that component.

Links are provided by both the Hyperstack::Router and Hyperstack::Router::Helper modules.

The Link method takes a url path, and these options:

  • search: String adds the specified string to the search query

  • hash: String adds the specified string to the hash location

    it can also take a block of children to render inside it.

NavLinks are the same as Links, but will add styling attributes when it matches the current url

  • active_class: String adds the class to the link when the url matches

  • active_style: String adds the style to the link when the url matches

  • active: Proc A proc that will add extra logic to determine if the link is active

Pre-rendering

Pre-rendering is automatically taken care for you under the hood.

Setup

To setup HyperRouter:

  • Install the gem

  • Your page should render your router as its top-level-component (first component to be rendered on the page) - in the example below this would be AppRouter

  • You will need to configure your server to route all unknown routes to the client-side router (Rails example below)

With Rails

Assuming your router is called AppRouter, add the following to your routes.rb

Note:

root 'Hyperstack#AppRouter' is shorthand which will automagically create a Controller, View and launch AppRouter as the top-level Component. If you are rendering your Component via your own COntroller or View then ignore this line.

Example

Here is the basic JSX example that is used on the react-router site

And here is the same example in Hyperstack:

Last updated

Was this helpful?