Router for React Native based on exNavigator
Actions.login)Schema to define common properties for a group of screens. For example, you can define a "modal" Schema for screens that animate from the bottom of the screen.npm i react-native-router-flux --save
Router and child Route elements for your screens.Schema element to reduce repetition| Property | Type | Default | Description |
|---|---|---|---|
| header | object | null | optional header view |
| footer | object | null | optional footer view (e.g. tab bar) |
| hideNavBar | bool | false | hides navigation bar for every route |
| Property | Type | Default | Description |
|---|---|---|---|
| name | string | required | Will be used to call screen transition, for example, Actions.name(params). Must be unique. |
| component | React.Component | semi-required | The Component to be displayed. Not required when defining a nested Router or child, see example |
| type | string | optional | Defines how the new screen is added to the navigator stack. One of push, modal,actionSheet,replace, switch, reset transitionToTop. Default is 'push'. replace tells navigator to replace current route with new route. actionSheet shows Action Sheet popup, you must pass callback as callback function, check Example for details. modal type inserts its 'component' after navigator component. It could be used for popup alerts as well for various needed processes before any navigator transitions (like login auth process).switch is used for tab screens. reset is similar to replace except it unmounts the componets in the navigator stack. modal component could be dismissed by using Actions.dismiss() transitionToTop will reset router stack ['route.name'] and with animation, if route has sceneConfig. eg <Route name="login" schema="modal" component={Login} type="transitionToTop" /> |
| initial | bool | false | Set to true if this is the initial screen |
| title | string | null | The title to be displayed in the navigation bar |
| schema | string | optional | Set this property to the name of a previously defined Schema to inherit its properties |
| wrapRouter | bool | false | If true, the route is automatically nested in its own Router. Useful for modal screens. For type==switch wrapRouter will be true |
| sceneConfig | Navigator.SceneConfigs | optional | Defines the transition animation. |
| defaultRoute | string | optional | Defines which route to go to if this route is used as a tab and the tab is clicked on when the tab is already selected |
| hideNavBar | bool | false | hides navigation bar for this route |
| hideTabBar | bool | false | hides tab bar for this route (if built-in TabBar component is used as footer for parent Router, check Example) |
| navigationBarStyle | View style | optional style override for the navigation bar | |
| titleStyle | Text style | optional style override for the title element | |
| renderTitle | Closure | optional closure to render the title element | |
| barButtonIconStyle | Image style | optional style override for icon buttons (e.g. back icon) | |
| leftTitle | string | optional string to display on the left if the previous route does not provide renderBackButton prop. renderBackButton > leftTitle > title> |
|
| renderLeftButton | Closure | optional closure to render the left title / buttons element | |
| renderBackButton | Closure | optional closure to render back text or button if this route happens to be the previous route | |
| leftButtonStyle | View style | optional style override for the container of left title / buttons | |
| leftButtonTextStyle | Text style | optional style override for the left title element | |
| rightTitle | string | optional string to display on the right. onRight must be provided for this to appear. |
|
| renderRightButton | Closure | optional closure to render the right title / buttons element | |
| rightButtonStyle | View style | optional style override for the container of right title / buttons | |
| rightButtonTextStyle | Text style | optional style override for the right title element |
| Property | Type | Default | Description |
|---|---|---|---|
| name | string | required | The name of the schema, to be referenced in the route as schema={"name"} |
| property | - | - | A Schema can have any property that you want the Route to inherit |

import React, {AppRegistry, Navigator, StyleSheet, Text, View} from 'react-native'
import Launch from './components/Launch'
import Register from './components/Register'
import Login from './components/Login'
import Login2 from './components/Login2'
import {Router, Route, Schema, Animations, TabBar} from 'react-native-router-flux'
import Error from './components/Error'
import Home from './components/Home'
import TabView from './components/TabView'
class TabIcon extends React.Component {
render(){
return (
<Text style={{color: this.props.selected ? 'red' :'black'}}>{this.props.title}</Text>
);
}
}
export default class Example extends React.Component {
render() {
return (
<Router hideNavBar={true}>
<Schema name="modal" sceneConfig={Navigator.SceneConfigs.FloatFromBottom}/>
<Schema name="default" sceneConfig={Navigator.SceneConfigs.FloatFromRight}/>
<Schema name="withoutAnimation"/>
<Schema name="tab" type="switch" icon={TabIcon} />
<Route name="launch" component={Launch} initial={true} wrapRouter={true} title="Launch"/>
<Route name="register" component={Register} title="Register"/>
<Route name="home" component={Home} title="Replace" type="replace"/>
<Route name="login" schema="modal">
<Router>
<Route name="loginModal" component={Login} title="Login" schema="modal"/>
<Route name="loginModal2" component={Login2} title="Login2"/>
</Router>
</Route>
<Route name="error" component={Error} title="Error" type="modal"/>
<Route name="register2" component={Register} title="Register2" schema="withoutAnimation"/>
<Route name="tabbar">
<Router footer={TabBar} hideNavBar={true} tabBarStyle={{borderTopColor:'#00bb00',borderTopWidth:1,backgroundColor:'white'}}>
<Route name="tab1" schema="tab" title="Tab #1" defaultRoute='tab1_1'>
<Router>
<Route name="tab1_1" component={TabView} title="Tab #1_1" />
<Route name="tab1_2" component={TabView} title="Tab #1_2" />
</Router>
</Route>
<Route name="tab2" schema="tab" title="Tab #2" hideNavBar={true}>
<Router onPop={()=>{console.log("onPop is called!"); return true} }>
<Route name="tab2_1" component={TabView} title="Tab #2_1" />
<Route name="tab2_2" component={TabView} title="Tab #2_2" />
</Router>
</Route>
<Route name="tab3" schema="tab" title="Tab #3" component={TabView} hideTabBar={true}/>
<Route name="tab4" schema="tab" title="Tab #4" component={TabView} />
<Route name="tab5" schema="tab" title="Tab #5" component={TabView} />
</Router>
</Route>
</Router>
);
}
}
components/Launch.js (initial screen)
import React, {View, Text, StyleSheet, TouchableHighlight} from 'react-native'
import Button from 'react-native-button'
import {Actions} from 'react-native-router-flux'
class Launch extends React.Component {
render(){
return (
<View style={styles.container}>
<Text>Launch page</Text>
<Button onPress={()=>Actions.login({data:"Custom data", title:'Custom title' })}>Go to Login page</Button>
<Button onPress={Actions.register}>Go to Register page</Button>
<Button onPress={Actions.register2}>Go to Register page without animation</Button>
<Button onPress={Actions.error('error message')}>Show error popup</Button>
<Button onPress={Actions.tabbar}>Go to TabBar page</Button>
</View>
);
}
}
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'transparent',
}
});
module.exports = Launch;
To display a modal use type="modal" for your route components.
Modal type inserts its 'component' after navigator component. See the Examples folder for more details.
Note that ReactNativeRouterFlux will not provide animations for modals and you'll need to animate the modal yourself (or use a library)
You can easily configure react-native-router-flux to handle a sidebar/drawer for specific routes:
1. Create a sidebar/drawer component (you can use both react-native-drawer and react-native-side-menu) and pass its router props to its children:
<DrawerLayout>
{React.Children.map(children, c => React.cloneElement(c, {route: this.props.route}))}
</DrawerLayout>
2. In you router component add the sidebar/drawer and nested routes following this pattern:
<Router>
<Route name="without-drawer"/>
<Route name="main">
<Drawer>
<Router>
<Route name="with-drawer-a"/>
<Route name="with-drawer-b"/>
</Router>
</Drawer>
</Route>
</Router>
There is an example which is worked together with react-native-drawer.
Also here is another complete example of a working drawer using a common app with: - A session screen that just checks if the user is already logged-in on startup (SessionScreen) - An auth screen that handles signin/signup (AuthScreen) - Many other screens that show the true content of the app, all using the sidebar
SideDrawer.js (I'm using 'react-native-drawer')
import Drawer from 'react-native-drawer'
class SideDrawer extends React.Component {
render() {
return (
<Drawer
type="overlay"
content={<SideDrawerContent />}
tapToClose={true}
openDrawerOffset={0.2}
panCloseMask={0.2}
closedDrawerOffset={-3}
styles={{ drawer: drawerStyle, main: mainStyle }}
tweenHandler={(ratio) => ({ main: { opacity: (2 - ratio) / 2 } })}
>
{React.Children.map(this.props.children, c => React.cloneElement(c, {
route: this.props.route
}))}
</Drawer>
)
}
}
Router.js ```javascript const hideNavBar = Platform.OS === 'android' const paddingTop = Platform.OS === 'android' ? 0 : 8
export default class Routes extends React.Component { render() { return (
<Route schema='boot' component={SessionScreen} name='session' initial={true} />
<Route schema='boot' component={AuthScreen} name='auth' />
<Route name='main' hideNavBar={true} type='reset'>
<SideDrawer>
<Router
sceneStyle={styles.scene}
navigationBarStyle={styles.navigationBar}
titleStyle={styles.title}
barButtonIconStyle={styles.barButtonIcon}
barButtonTextStyle={styles.barButtonText}
>
<Route schema='main' component={PlaceScreen} name='place' title='Places' />
<Route schema='main' component={PaymentScreen} name='payment' title='Payment' header={Toolbar} />
</Router>
$ claude mcp add react-native-router-flux \
-- python -m otcore.mcp_server <graph>