Summary of react-navigation usage (with code)
This time I will bring you a summary of the use of react-navigation (with code), what are the precautions when using react-navigation, the following is a practical case, let's take a look.
After reading the navigator comparison in the official documents, I found that the currently recommended solution is a separate navigation library react-navigation. It is said that its use is very simple. I wrote a demo and tried it.
1. Main components
It is mainly divided into three parts according to the usage form:
StackNavigator: Similar to an ordinary Navigator, the navigation bar at the top of the screen
TabNavigator: is equivalent to the TabBarController in ios, and the tab bar at the bottom of the screen
DrawerNavigator: Drawer effect, slide out from the side
2. Use
1. Create a new project
react-native init ComponentDemo
2. Install this library in the application
npm install --save react-navigation
After installation, I found that it is a beta version (v1.0.0-beta.7), but there is a pitfall?! We will talk about this pitfall in detail in a moment~
3. Test TabNavigator, StackNavigator and DrawerNavigator
(1) Create a new HomePage.js
import React from 'react'; import { StyleSheet, View, Text, Button, Image } from 'react-native'; import { StackNavigator, TabNavigator } from 'react-navigation'; import ChatScreen from './ChatScreen'; import MinePage from './MinePage'; class HomePage extends React.Component{ static navigationOptions={ title: '首页',//设置标题内容 header:{ backTitle: ' ',//返回按钮标题内容(默认为上一级标题内容) } } constructor(props) { super(props); } render() { const {navigate} = this.props.navigation; return ( <View style={styles.container}> <Text style={{padding:10}}>Hello, Navigation!</Text> <Button onPress={() => navigate('Chat',{user:'Sybil'})} title="点击跳转"/> </View> ) } } const MainScreenNavigator = TabNavigator({ Home: { screen: HomePage, navigationOptions: { tabBar: { label: '首页', icon: ({tintColor}) => ( <Image source={require('./image/bar_home_nomarl@3x.png')} style={[{tintColor: tintColor},styles.icon]} /> ), }, } }, Certificate: { screen: MinePage, navigationOptions: { tabBar: { label: '我的', icon: ({tintColor}) => ( <Image source={require('./image/bar_center_normal@3x.png')} style={[{tintColor: tintColor},styles.icon]} /> ), }, } }, }, { animationEnabled: false, // 切换页面时不显示动画 tabBarPosition: 'bottom', // 显示在底端,android 默认是显示在页面顶端的 swipeEnabled: false, // 禁止左右滑动 backBehavior: 'none', // 按 back 键是否跳转到第一个 Tab, none 为不跳转 tabBarOptions: { activeTintColor: '#008AC9', // 文字和图片选中颜色 inactiveTintColor: '#999', // 文字和图片默认颜色 showIcon: true, // android 默认不显示 icon, 需要设置为 true 才会显示 indicatorStyle: {height: 0}, // android 中TabBar下面会显示一条线,高度设为 0 后就不显示线了 style: { backgroundColor: '#fff', // TabBar 背景色 }, labelStyle: { fontSize: 12, // 文字大小 }, }, }); const styles = StyleSheet.create({ container:{ flex: 1, backgroundColor:'#fff' }, icon: { height: 22, width: 22, resizeMode: 'contain' } }); const SimpleApp = StackNavigator({ Home: {screen: MainScreenNavigator}, Chat:{screen:ChatScreen}, }); export default SimpleApp;
(2) Create a new ChatScreen.js
import React from 'react'; import { Button, Image, View, Text } from 'react-native'; class ChatScreen extends React.Component { static navigationOptions = { title:'聊天', }; render() { const {params} = this.props.navigation.state; return ( <View style={{backgroundColor:'#fff',flex:1}}> <Text style={{padding:20}}>Chat with {params.user}</Text> </View> ); } } export default ChatScreen;
(3) Create a new MinePage.js
import React,{Component} from 'react'; import { Button, Image, View, Text, StyleSheet } from 'react-native'; import { DrawerNavigator } from 'react-navigation'; import MyNotificationsScreen from './MyNotificationsScreen'; class MinePage extends Component{ static navigationOptions = { title:'我的', drawerLabel: '我的', // Note: By default the icon is only shown on iOS. Search the showIcon option below. drawerIcon: ({ tintColor }) => ( <Image source={require('./image/chat@3x.png')} style={[styles.icon, {tintColor: tintColor}]} /> ), }; render(){; return( <View style={{backgroundColor:'#fff',flex:1}}> <Text style={{padding:20}}>Sybil</Text> <Button style={{padding:20}} onPress={() => this.props.navigation.navigate('DrawerOpen')} title="点击打开侧滑菜单" /> </View> ); } } const styles = StyleSheet.create({ icon: { width: 24, height: 24, }, }); const MyChatNavigator = DrawerNavigator({ MyChat: { screen: MinePage, }, Notifications: { screen: MyNotificationsScreen, }, },{ drawerWidth: 220, // 抽屉宽 drawerPosition: 'left', // 抽屉在左边还是右边 // contentComponent: CustomDrawerContentComponent, // 自定义抽屉组件 contentOptions: { initialRouteName: MinePage, // 默认页面组件 activeTintColor: '#008AC9', // 选中文字颜色 activeBackgroundColor: '#f5f5f5', // 选中背景颜色 inactiveTintColor: '#000', // 未选中文字颜色 inactiveBackgroundColor: '#fff', // 未选中背景颜色 style: { // 样式 } } }); export default MyChatNavigator;
(4) Write MyNotificationsScreen.js
import React from 'react'; import { StyleSheet, View, Text, Button, Image } from 'react-native'; class MyNotificationsScreen extends React.Component { static navigationOptions = { title:'通知', drawerLabel: '通知', drawerIcon: ({ tintColor }) => ( <Image source={require('./image/notif@3x.png')} style={[styles.tabIcon, {tintColor: tintColor}]} /> ), }; render() { return ( <View style={{backgroundColor:'#fff'}}> <Button style={{padding:20}} onPress={() => this.props.navigation.navigate('DrawerOpen')} title="点击打开侧滑菜单" /> <Button onPress={() => this.props.navigation.goBack()} title="返回我的界面" /> </View> ); } } const styles = StyleSheet.create({ tabIcon: { width: 24, height: 24, }, }); export default MyNotificationsScreen;
(5) Run
and report an error? This is the pit we mentioned above~
What is the reason? It turns out to be a bug in the beta version. Find line 12 of node_modules/react-navigation/src/views/Header.js in the directory. Delete it and it will be OK~
Ps: Unfortunately, I don’t have this error. Leave a picture~ When I am about to publish this article, the latest version has changed to (v1.0.0-beta.9), and the latest version has modified the above bug!
Okay, run it again~
Last dynamic rendering:
I believe you have mastered the method after reading the case in this article. For more exciting content, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Detailed explanation of the steps to implement website login with node puppeteer (with code)
Vue data monitoring watch usage instructions
The above is the detailed content of Summary of react-navigation usage (with code). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

CrystalDiskMark is a small HDD benchmark tool for hard drives that quickly measures sequential and random read/write speeds. Next, let the editor introduce CrystalDiskMark to you and how to use crystaldiskmark~ 1. Introduction to CrystalDiskMark CrystalDiskMark is a widely used disk performance testing tool used to evaluate the read and write speed and performance of mechanical hard drives and solid-state drives (SSD). Random I/O performance. It is a free Windows application and provides a user-friendly interface and various test modes to evaluate different aspects of hard drive performance and is widely used in hardware reviews

foobar2000 is a software that can listen to music resources at any time. It brings you all kinds of music with lossless sound quality. The enhanced version of the music player allows you to get a more comprehensive and comfortable music experience. Its design concept is to play the advanced audio on the computer The device is transplanted to mobile phones to provide a more convenient and efficient music playback experience. The interface design is simple, clear and easy to use. It adopts a minimalist design style without too many decorations and cumbersome operations to get started quickly. It also supports a variety of skins and Theme, personalize settings according to your own preferences, and create an exclusive music player that supports the playback of multiple audio formats. It also supports the audio gain function to adjust the volume according to your own hearing conditions to avoid hearing damage caused by excessive volume. Next, let me help you

What to do with blue screen code 0x0000001? The blue screen error is a warning mechanism when there is a problem with the computer system or hardware. Code 0x0000001 usually indicates a hardware or driver failure. When users suddenly encounter a blue screen error while using their computer, they may feel panicked and at a loss. Fortunately, most blue screen errors can be troubleshooted and dealt with with a few simple steps. This article will introduce readers to some methods to solve the blue screen error code 0x0000001. First, when encountering a blue screen error, we can try to restart

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

If you need to program any device remotely, this article will help you. We will share the top GE universal remote codes for programming any device. What is a GE remote control? GEUniversalRemote is a remote control that can be used to control multiple devices such as smart TVs, LG, Vizio, Sony, Blu-ray, DVD, DVR, Roku, AppleTV, streaming media players and more. GEUniversal remote controls come in various models with different features and functions. GEUniversalRemote can control up to four devices. Top Universal Remote Codes to Program on Any Device GE remotes come with a set of codes that allow them to work with different devices. you may

After long pressing the play button of the speaker, connect to wifi in the software and you can use it. Tutorial Applicable Model: Xiaomi 12 System: EMUI11.0 Version: Xiaoai Classmate 2.4.21 Analysis 1 First find the play button of the speaker, and press and hold to enter the network distribution mode. 2 Log in to your Xiaomi account in the Xiaoai Speaker software on your phone and click to add a new Xiaoai Speaker. 3. After entering the name and password of the wifi, you can call Xiao Ai to use it. Supplement: What functions does Xiaoai Speaker have? 1 Xiaoai Speaker has system functions, social functions, entertainment functions, knowledge functions, life functions, smart home, and training plans. Summary/Notes: The Xiao Ai App must be installed on your mobile phone in advance for easy connection and use.
