Home Web Front-end JS Tutorial react-navigation use case analysis

react-navigation use case analysis

Jun 01, 2018 am 10:42 AM
use parse

This time I will bring you an analysis of react-navigation use cases. What are the precautions when using react-navigation? The following is a practical case, let’s take a look.

1. Main components

It is mainly divided into three parts according to the usage form:

  1. StackNavigator: Similar to an ordinary Navigator, the navigation bar at the top of the screen

  2. TabNavigator: is equivalent to the TabBarController in ios, and the tab bar at the bottom of the screen

  3. DrawerNavigator: Drawer effect, slide out from the side

2. Use

1. Create a new project

1

react-native init ComponentDemo

Copy after login

2. Install this library in the application

1

npm install --save react-navigation

Copy after login

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

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(&#39;./image/bar_home_nomarl@3x.png&#39;)}

            style={[{tintColor: tintColor},styles.icon]}

          />

        ),

      },

    }

  },

  Certificate: {

    screen: MinePage,

    navigationOptions: {

      tabBar: {

        label: '我的',

        icon: ({tintColor}) => (

          <Image

            source={require(&#39;./image/bar_center_normal@3x.png&#39;)}

            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;

Copy after login

(2) Create a new ChatScreen.js

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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:&#39;#fff&#39;,flex:1}}>

      <Text style={{padding:20}}>Chat with {params.user}</Text>

    </View>

    );

  }

}

export default ChatScreen;

Copy after login

(3) Create a new MinePage.js

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

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(&#39;./image/chat@3x.png&#39;)}

      style={[styles.icon, {tintColor: tintColor}]}

     />

   ),

  };

  render(){;

    return(

      <View style={{backgroundColor:&#39;#fff&#39;,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;

Copy after login

(4) Write MyNotificationsScreen.js

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

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(&#39;./image/notif@3x.png&#39;)}

        style={[styles.tabIcon, {tintColor: tintColor}]}

      />

    ),

  };

  render() {

    return (

       <View style={{backgroundColor:&#39;#fff&#39;}}>

        <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;

Copy after login

(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:

How to use vue to implement the 2048 mini game

How to use VeeValidate to perform form validation in the vue project Check function

The above is the detailed content of react-navigation use case analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What software is crystaldiskmark? -How to use crystaldiskmark? What software is crystaldiskmark? -How to use crystaldiskmark? Mar 18, 2024 pm 02:58 PM

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

How to download foobar2000? -How to use foobar2000 How to download foobar2000? -How to use foobar2000 Mar 18, 2024 am 10:58 AM

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

How to use Baidu Netdisk app How to use Baidu Netdisk app Mar 27, 2024 pm 06:46 PM

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.

How to use NetEase Mailbox Master How to use NetEase Mailbox Master Mar 27, 2024 pm 05:32 PM

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

BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? Apr 26, 2024 am 09:40 AM

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

Detailed explanation of Oracle error 3114: How to solve it quickly Detailed explanation of Oracle error 3114: How to solve it quickly Mar 08, 2024 pm 02:42 PM

Detailed explanation of Oracle error 3114: How to solve it quickly, specific code examples are needed. During the development and management of Oracle database, we often encounter various errors, among which error 3114 is a relatively common problem. Error 3114 usually indicates a problem with the database connection, which may be caused by network failure, database service stop, or incorrect connection string settings. This article will explain in detail the cause of error 3114 and how to quickly solve this problem, and attach the specific code

Teach you how to use the new advanced features of iOS 17.4 'Stolen Device Protection' Teach you how to use the new advanced features of iOS 17.4 'Stolen Device Protection' Mar 10, 2024 pm 04:34 PM

Apple rolled out the iOS 17.4 update on Tuesday, bringing a slew of new features and fixes to iPhones. The update includes new emojis, and EU users will also be able to download them from other app stores. In addition, the update also strengthens the control of iPhone security and introduces more "Stolen Device Protection" setting options to provide users with more choices and protection. "iOS17.3 introduces the "Stolen Device Protection" function for the first time, adding extra security to users' sensitive information. When the user is away from home and other familiar places, this function requires the user to enter biometric information for the first time, and after one hour You must enter information again to access and change certain data, such as changing your Apple ID password or turning off stolen device protection.

What is chirp down? -How to use chirp down What is chirp down? -How to use chirp down Mar 18, 2024 am 11:46 AM

Chirp Down can also be called JJDown. This is a video download tool specially created for Bilibili. However, many friends do not understand this software. Today, let the editor explain to you what Chirp Down is? How to use chirp down. 1. The origin of Chirpdown Chirpdown originated in 2014. It is a very old video downloading software. The interface adopts Win10 tile style, which is simple, beautiful and easy to operate. Chirna is the poster girl of Chirpdown, and the artist is あさひクロイ. Jijidown has always been committed to providing users with the best download experience, constantly updating and optimizing the software, solving various problems and bugs, and adding new functions and features. The function of Chirp Down Chirp Down is

See all articles