Guide
- Installation
- Introduction
- Getting Started
- Declarative Rendering
- Basic Components
- Conditionals and Loops
- Composing with components
- Handling user input
- Animations
- Slots
- Component As Prop
- Device APIs
- React Native
- Ready for more?
- Vue Native Router
- Testing
- Community Libraries
- How does it work?
- How to contribute
- FAQ
Vue Native Router
Navigation in apps
Very soon you’ll find yourself in a position where your app has become large enough to have more than a few screens. At this point you’ll need a way to navigate between them. It would be nice to have some mechanism to move between screens while keeping track of which screens you visited, where you came from and maybe even a drawer to quickly go to a specific screen.
Vue Native Router takes care of these requirements. It provides utilities to create navigators like stack navigators and drawer navigators, and a Vue plugin for easily controlling navigation from within components.
Since it’s based on React Navigation, most of the API and components provided by it can be used in Vue Native.
Installation
You can install the package using the command$ npm install vue-native-router
NOTE
Some of its dependencies assume that the following packages are already installed:
So if you don’t have them installed, use this command to install them:
$ npm install -g react-native-reanimated react-native-gesture-handler react-native-paper |
An additional step is required to link these dependencies if you are using just React Native 0.59 without Expo. Refer to this article for details.
Basic navigation with a stack navigator
A stack navigator provides a way for your app to transition between screens in such a way that each new screen is placed on top of a stack. createStackNavigator
makes such a navigator for us. Here is a code snippet that shows how you can use a stack navigator in your app with two screens.
<template> |
IMPORTANT
Although there are several navigators provided by vue-native-router
, the top level navigator must always be wrapped in an app container.
Moving between screens
Since the navigation is available as prop within the child components. We can use something like this.navigation.navigate("Home");
to switch from one screen to another.
You can also pass data to routes when we navigate as follows
this.navigation.navigate("RouteName", {/* options go here */}); |
<!-- screens/DetailsScreen.vue --> |
Going back
It is possible to go back from the active screen if there is any. We can trigger it using this.navigation.goBack()
.
For the full code with usage of createStackNavigator
refer to this example.
Drawer navigation
For Drawer support, you just need to add screen Drawer
in createStackNavigator
options.
You can initialize the drawer using createDrawerNavigator
and create a sidebar component to render the sidebar view.
const DrawerNavigator = createDrawerNavigator( |
Then use the AppNavigator
the same way as shown earlier.
<template> |
For the full code with usage of createDrawerNavigator
refer to this example.
Tab navigation
Another common style of navigation in mobile apps is tab-based navigation.
You can use createBottomTabNavigator
and createMaterialTopTabNavigator
to implement tab based routing.
import { |
For the full code with usage of createBottomTabNavigator
and createMaterialTopTabNavigator
refer to this example.
Further information
Since the app is based on React Navigation v3, you can use most the API’s specified in the react navigation docs at https://reactnavigation.org/docs/en/getting-started.html.
Vue Native KitchenSink is an sample app which uses Vue Native Router and demonstrates Native Base component usage in Vue Native apps.