logo

The Navigation router

Scene-Based Navigation for React and React Native

Scene-Based Navigation

Native apps have always had scene-based navigation. The Navigation router is the first to bring it to the web. You give the Navigation router a list of your scenes. After you navigate to a scene, the Navigation router gets out of your way so you can build your UI however you want.

React and React Native

You don't need a different routing library for React and React Native anymore. The Navigation router works on both. What's more, it doesn't compromise the UX. On React Native, the navigation is 100% native on Android and iOS. On React, you can have whatever URLs you want.

web

React

native React Native
  1. Define Your States

    import { StateNavigator } from 'navigation';
    
    const stateNavigator = new StateNavigator([
      { key: 'hello', route: '' },
      { key: 'world' }
    ]);

    You create one State for each scene (page) in your app. You don't need to define your routes yet. The Navigation router generates interim routes. You can define your real routes at any time without changing any code. With scene-based navigation, there aren't any hard-coded Urls for you to update.

    Next - create your scenes
  2. Create Your Scenes

    const { hello, world } = stateNavigator.states;
    
    hello.renderScene = () => <Hello />;
    world.renderScene = () => <World />;

    For each scene, you create a component that renders its UI. You map these scene components to their corresponding States. All the other routers for React force you to think in terms of routes. But this is confusing because routes and scenes aren't the same thing. One scene can have more than one route, for example, a master/details page.

    Next - navigate to a scene
  3. Navigate to a Scene

    import { NavigationLink } from 'navigation-react';
    
    const Hello = () => (
      <NavigationLink
        stateKey="world"
        navigationData={{ size: 20 }}>
        Hello
      </NavigationLink>
    );

    The NavigationLink component changes scene. You pass the name of the scene and the data. The Navigation router builds the Url. If you've configured more than one route it uses the best match.

    Next - use the data
  4. Use the Data

    import { NavigationContext } from 'navigation-react';
    
    const World = () => {
      const { data } = useContext(NavigationContext);
      return (
        <div style={{ fontSize: data.size }}>
          World
        </div>
      );
    };

    In the next scene, you access the data from the NavigationContext. The Navigation router passes strongly-typed data. Here, the size is a number.

Take a look at the complete Hello World example