Home  >  Article  >  Web Front-end  >  react-navigation use case analysis

react-navigation use case analysis

php中世界最好的语言
php中世界最好的语言Original
2018-06-01 10:42:241831browse

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

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 (
      
        Hello, Navigation!
        

(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 (
    
      Chat with {params.user}
    
    );
  }
}
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 }) => (
     
   ),
  };
  render(){;
    return(
      
        Sybil
        

(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 }) => (
      
    ),
  };
  render() {
    return (
       
        

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