替代 Navigator 组件
前言:最近在研究 react-native 时,突然发现 Navigator 组件被 react-native 包 抛弃了。现总结了几种替代方法。
方法一:引入 react-native-deprecated-custom-components 组件
npm install react-native-deprecated-custom-components --save
import CustomerComponents, { Navigator } from 'react-native-deprecated-custom-components'; // 引入
方法二:引入 react-navigation 组件
npm install react-navigation --save
官网:https://reactnavigation.org/docs/intro/
demo-1
BasicApp.js
import {
  StackNavigator,
} from 'react-navigation';
const BasicApp = StackNavigator({
  Main: {screen: MainScreen},
  Profile: {screen: ProfileScreen},
});
MainScreen.js
class MainScreen extends React.Component {
  static navigationOptions = {
    title: 'Welcome',
  };
  render() {
    const { navigate } = this.props.navigation;
    return (
      <Button
        title="Go to Jane's profile"
        onPress={() =>
          navigate('Profile', { name: 'Jane' })
        }
      />
    );
  }
}
ProfileScreen.js
class ProfileScreen extends React.Component {
  static navigationOptions = ({navigation}) => ({
    title: navigation.state.params.name,
  });
  render() {
    const { goBack } = this.props.navigation;
    return (
      <Button
        title="Go back"
        onPress={() => goBack()}
      />
    );
  }
}
效果图:
android

ios

demo-2:
BasicApp.js
import {
  TabNavigator,
} from 'react-navigation';
const BasicApp = TabNavigator({
  Main: {screen: MainScreen},
  Setup: {screen: SetupScreen},
});
MainScreen.js
class MainScreen extends React.Component {
  static navigationOptions = {
    tabBarLabel: 'Home',
  };
  render() {
    const { navigate } = this.props.navigation;
    return (
      <Button
        title="Go to Setup Tab"
        onPress={() => navigate('Setup')}
      />
    );
  }
}
SetupScreen.js
class SetupScreen extends React.Component {
  static navigationOptions = {
    tabBarLabel: 'Setup',
  };
  render() {
    const { goBack } = this.props.navigation;
    return (
      <Button
        title="Go back to home tab"
        onPress={() => goBack()}
      />
    );
  }
}
效果:
android

ios

方法三:自定义 Navigator 组件
首先导入组件
var MLNavigator = require('../Lib/MLNavigator');
然后使用
<MLNavigator
leftIconName = 'nav_btn_back'
title = '我的导航'
rightIconName = 'nav_btn_back'
rightTitle = '右边标题'
callBackLeftClick = {()=> this.popToHome()}
callBackRightClick = {()=> this.popToHome()}
/>
定义的一些属性:
leftIconName: '', // 左边图片
leftTitle: '', // 左边标题
title: '', // 标题
rightIconName: '', // 右边图片
rightTitle: '', // 右边标题
callBackLeftClick: null, // 左边回调
callBackRightClick: null, // 右边回调
leftTitleFontSize: 14, // 左边标题的字体大小
titleFontSize: 16, // 标题的字体大小
rightTitleFontSize: 14, // 右边标题的字体大小
leftTitleColor: '#666666', // 左边标题的字体颜色
titleColor: 'black', // 标题的字体颜色
rightTitleColor: '#666666', // 右边标题的字体颜色
组件封装:
import React, { Component } from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    Image,
    Platform,
    TouchableOpacity
} from 'react-native';
var Dimensions = require('Dimensions');
var width = Dimensions.get('window').width;
var Height = Dimensions.get('window').height;
var MLNavigator = React.createClass ({
    getDefaultProps() {
        return{
            leftIconName: '',    // 左边图片
            leftTitle: '',       // 左边标题
            title: '',           // 标题
            rightIconName: '',   // 右边图片
            rightTitle: '',      // 右边标题
            callBackLeftClick: null,   // 左边回调
            callBackRightClick: null,  // 右边回调
            leftTitleFontSize: 14,     // 左边标题的字体大小
            titleFontSize: 16,         // 标题的字体大小
            rightTitleFontSize: 14,    // 右边标题的字体大小
            leftTitleColor: '#666666',     // 左边标题的字体颜色
            titleColor: 'black',         // 标题的字体颜色
            rightTitleColor: '#666666',    // 右边标题的字体颜色
        }
    },
    render() {
        return (
            <View style={styles.NavBarStytle}>
                {/* 左边 */}
                {this.navLeftView()}
                <Text style={{color: this.props.titleColor, fontSize: this.props.titleFontSize, fontWeight: 'bold', bottom:-10}}>{this.props.title}</Text>
                {/* 右边 */}
                {this.navRightView()}
            </View>
        );
    },
    navLeftView() {
        if(this.props.leftIconName){
            return(
                <TouchableOpacity activeOpacity={0.5} style={styles.leftViewStytle} onPress={()=> {this.props.callBackLeftClick()}}>
                    <Image source={{uri: this.props.leftIconName}} style={styles.NavLeftImageStyle} />
                </TouchableOpacity>
            )
        }else {
            return(
                <TouchableOpacity activeOpacity={0.5} style={styles.leftViewStytle} onPress={()=> {this.props.callBackLeftClick()}}>
                    <Text style={{color: this.props.leftTitleColor, fontSize: this.props.leftTitleFontSize, bottom:-2}}>{this.props.rightTitle}</Text>
                </TouchableOpacity>
            )
        }
    },
    navRightView() {
        if(this.props.rightIconName){
            return(
                <TouchableOpacity activeOpacity={0.5} style={styles.rightViewStytle} onPress={()=> {this.props.callBackRightClick()}}>
                    <Image source={{uri: this.props.rightIconName}} style={styles.NavRightImageStyle} />
                </TouchableOpacity>
        )
        }else {
            return(
                <TouchableOpacity activeOpacity={0.5} style={styles.rightViewStytle} onPress={()=> {this.props.callBackRightClick()}}>
                    <Text style={{color: this.props.rightTitleColor, fontSize: this.props.rightTitleFontSize, bottom:-2}}>{this.props.rightTitle}</Text>
                </TouchableOpacity>
             )
        }
    },
})
const styles = StyleSheet.create({
    NavBarStytle: {
        width: width,
        height: Platform.OS == 'ios' ? 64 : 44,
        backgroundColor: '#F2F2F2',
        flexDirection: 'row',
        alignItems: 'center',
        justifyContent: 'center'
    },
    leftViewStytle: {
        position: 'absolute',
        left: 15,
        bottom: 15
    },
    NavLeftImageStyle: {
        width: Platform.OS == 'ios' ? 15 : 15,
        height: Platform.OS == 'ios' ? 15 : 15,
    },
    rightViewStytle: {
        position: 'absolute',
        right: 15,
        bottom: 15
    },
    NavRightImageStyle: {
        width: Platform.OS == 'ios' ? 15 : 15,
        height: Platform.OS == 'ios' ? 15 : 15,
    },
});
module.exports = MLNavigator;
.
替代 Navigator 组件的更多相关文章
- 微信小程序把玩(二十六)navigator组件
		原文:微信小程序把玩(二十六)navigator组件 navigator跳转分为两个状态一种是关闭当前页面一种是不关闭当前页面.用redirect属性指定. 主要属性: wxml <naviga ... 
- ReactNative: 使用导航栏组件-NavigatorIOS组件和Navigator组件
		一.简言 在软件开发中,不论是Web还是App,它们的应用程序都是由很多的功能视图组成的.对于这些组合的视图,如何实现页面间平滑地过渡,应用都有统一的一套跳转机制,这个功能就是路由或者叫导航.应用程序 ... 
- Flutter中的普通路由与命名路由(Navigator组件)
		Flutter 中的路由通俗的讲就是页面跳转.在 Flutter 中通过 Navigator 组件管理路由导航.并提供了管理堆栈的方法.如:Navigator.push 和 Navigator.pop ... 
- navigator组件(相当于a标签)
		navigator组件:页面链接: navigator组件属性: target:类型 字符串 在哪个目标上发生跳转,默认当前小程序 属性值:self 当前小程序 miniProgram 其他小程序 u ... 
- React Native常用组件之TabBarIOS、TabBarIOS.Item组件、Navigator组件、NavigatorIOS组件、React Navigation第三方
		以下内容为老版本React Native,faceBook已经有了新的导航组件,请移步其他博客参考>>[我是传送门] 参考资料:React Navigation react-native ... 
- react native的Navigator组件示例
		import React, {Component} from 'react';import {ScrollView, StyleSheet, Text, View, PixelRatio} from ... 
- Unity TextMeshPro替代Text组件创建简体中文字体纹理集
		Unity原生的Text组件有一个毛病,只要文本放大字体放大就会有毛边或锯齿,一个更好的解决方案是用TextMeshPro替代ugui中的Text组件. TMPro采用SDF文字渲染技术,可以使文字放 ... 
- React Native使用Navigator组件进行页面导航报this.props....is not a function错误
		在push的时候定义回调函数: this.props.navigator.push({ component: nextVC, title: titleName, passProps: { //回调 g ... 
- React Native  Navigator组件回调
		在push的时候定义回调函数: this.props.navigator.push({ component: nextVC, title: titleName, passProps: { //回调 g ... 
随机推荐
- linq使用 count与sum等
			using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; us ... 
- 无线网络发射器选址 (NOIP2014)(真·纯模拟)
			原题传送门 好吧,如果说D1T1是纯模拟大水题 D2T1就是纯模拟略水题. 这道题首先我们要看一看数据范围.. 0<=n,m<=128 送分也不带这么送的吧.. 二维前缀和,前缀和,二次循 ... 
- I2C总线介绍
			1. 简介 I2C, Inter-Integrated Circuit, 是一种串行通信总线,用于连接微控制器及其外围设备它是一种两线式串行总线(串行数据:SDA; 串行时钟频率:SCL), 利用电阻 ... 
- redis 单机模拟 cluster集群
			一.redis-cluster设计 Redis集群搭建的方式有多种,例如使用zookeeper等,但从redis 3.0之后版本支持redis-cluster集群,Redis-Cluster采用无中心 ... 
- 加密中的salt是啥意思
			今天在stackoverflow上查看python的md5的问题,提到,除了简单的加密外,还可以加入一点salt 啥意思?百度一下看到:(https://zhidao.baidu.com/questi ... 
- hdu 5138(水题)
			题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5138 反着来. #include<iostream> #include<cstdi ... 
- codeforces-723D
			题目连接:http://codeforces.com/contest/723/problem/D D. Lakes in Berland time limit per test 2 seconds m ... 
- TCC分布式事务的实现原理(转载 石杉的架构笔记)
			拜托,面试请不要再问我TCC分布式事务的实现原理![石杉的架构笔记] 原创: 中华石杉 目录 一.写在前面 二.业务场景介绍 三.进一步思考 四.落地实现TCC分布式事务 (1)TCC实现阶段一:Tr ... 
- uva10857(状态压缩DP)
			uva10857 题意 兔子希望在平面上 n 个点上放蛋,每个点最多放一个蛋,初始兔子在 (0, 0) 点,这里有无数个蛋,兔子可以回到这个点取蛋,兔子的速度为 \(v * 2^{-i}\)(i 为携 ... 
- [POI2008]Mirror Trap
			题目大意: 一个$n(n\le10^5)$个顶点的格点多边形,每条边平行于网格线,每个角度数均为$90^\circ$或$270^\circ$,周长小于$3\times10^5$,每个顶点可以安装激光发 ... 
