Home

Awesome

React Native Router

Awesome navigation for your React Native app.

NOTE: This project is deprecated. Please use React Navigation instead!

Legacy instructions:

Install

Make sure that you are in your React Native project directory and run:

npm install react-native-router --save

Usage

var Router = require('react-native-router');

The basics:

// The initial page
var HelloPage = React.createClass({
  render: function() {
    return <Text>Hello world!</Text>;
  }
});

// Your route object should contain at least:
// - The name of the route (which will become the navigation bar title)
// - The component object for the page to render
var firstRoute = {
  name: 'Welcome!',
  component: HelloPage
};

// The Router wrapper
var MyApp = React.createClass({
  render() {
    return (
      <Router firstRoute={firstRoute} />
    )
  }
});

AppRegistry.registerComponent('routerTest', () => MyApp);

Boom. That's it.

From the "Hello world!"-page you can then navigate further to a new component by calling this.props.toRoute(). Let's build upon the HelloPage component in our first example:

var HelloPage = React.createClass({

  nextPage: function() {
    this.props.toRoute({
      name: "A new screen",
      component: HelloPage
    });
  },

  render: function() {
    return (
      <View>
        <TouchableHighlight onPress={this.nextPage} underlayColor="transparent">
          <Text>Next page please!</Text>
        </TouchableHighlight>
      </View>
    );
  }
});

Now, when you click on "Next page please!", it will go to the next page (which in this case is still HelloPage but with a new title). Keep in mind that this.props.toRoute() needs to be called from one of the top-level routes, therefore, if your link is deeply nested within multiple components, you need to make sure that the action "bubbles up" until it reaches the parent route, which in turn calls this.props.toRoute().

A more advanced example: Twitter app

To see more of the router in action, you can check out the Twitter example app that comes with the package. Just make sure that you first drag all the images from node_modules/react-native-router/twitter-example/images to your project's Images.xcassets:

Drag the assets to xcassets

After that, don't forget to rebuild the app in XCode before you launch the simulator. Then test the app by requiring the TwitterApp component:

var TwitterApp = require('./node_modules/react-native-router/twitter-example');

var {
  AppRegistry
} = React;

AppRegistry.registerComponent('routerTest', () => TwitterApp);

Configurations

The <Router \> object used to initialize the navigation can take the following props:

The this.props.toRoute() callback prop takes one parameter (a JavaScript object) which can have the following keys:

Todos

When swiping from left to right to go back, it would be nice if the navigation bar's opacity gradually changed with the user's finger position. (See Issue #1)

Questions?

If something is unclear or if you just want to say hi, feel free to follow me on Twitter!