React Native StackNavigator 实现跳转返回

首先进入到项目的跟路径然后加载 react-navigation 第三方库 如图


React Native StackNavigator 实现跳转返回_第1张图片
6B4AEE6E-435E-4C54-B70E-C87235FAF6D9.png

代码中引入StackNavigator组件


React Native StackNavigator 实现跳转返回_第2张图片
5CF902D1-9639-494D-8775-A9A87F376734.png

代码实现分三个js页面实现,index.ios.js MainVC.js首页 DetailVC.js详情页面
首先来看一下 index.ios.js实现代码
import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TouchableOpacity
} from 'react-native';
import {
  StackNavigator,
} from 'react-navigation';
import DetailVC from './DetailVC';
import MainVC from './MainVC';
// StackNavigator配置,默认显示MianVC页面
const MyNavigatior = StackNavigator(
  {
    MainVC: {screen: MainVC },
    DetailVC: {screen: DetailVC},
  },
  {
    initialRouteName: 'MainVC',//默认路由,就是第一个要显示的页面
  }
);


const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#F5FCFF',
  },
});

AppRegistry.registerComponent('MyNavigatior', () => MyNavigatior);

然后看MainVC.js首页实现代码

mport React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TouchableOpacity
} from 'react-native';
export default class MainVC extends Component {
  static navigationOptions = {
    title: '首页'//对页面的配置
  };
  render() {
    const { navigate } = this.props.navigation;
    return (
      
         navigate('DetailVC', { title: '详情',des:'我是返回点击我' })} >
           点击进详情页
        
      
    );
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#F5FCFF',
  },
});

在看DetailVC.js首页实现代码

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TouchableOpacity
} from 'react-native';
export default class DetailVC extends Component {
  //接收上一个页面传过来的title显示出来
  static navigationOptions = ({ navigation }) => ({
    title: navigation.state.params.title
  });
  // 点击返回上一页方法
  backVC=()=>{
    //返回首页方法
    this.props.navigation.goBack();
  }
  render() {
    const { navigate } = this.props.navigation;
    return (
        
          {this.backVC()}}>
             {this.props.navigation.state.params.des}
          
        
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#F5FCFF',
  },
});

最看看一下实现的效果图
iOS

QQ20170818-115330-HD.gif

Android

QQ20170818-115425-HD.gif

你可能感兴趣的:(React Native StackNavigator 实现跳转返回)