react-navigator

前端之家收集整理的这篇文章主要介绍了react-navigator前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

StackNavigator 导航栏

API: StackNavigator(RouteConfigs,StackNavigatorConfig)

  1. // 注册导航
  2. const Navs = StackNavigator({
  3. Home: { screen: Tabs },HomeTwo: {
  4. screen: HomeTwo,// 必须,其他都是非必须
  5. path:'app/homeTwo',使用url导航时用到,如 web app Deep Linking
  6. navigationOptions: {} // 此处设置了,会覆盖组件内的`static navigationOptions`设置. 具体参数详见下文
  7. },HomeThree: { screen: HomeThree },HomeFour: { screen: HomeFour }
  8. },{
  9. initialRouteName: 'Home',// 默认显示界面
  10. navigationOptions: { // 屏幕导航的默认选项,也可以在组件内用 static navigationOptions 设置(会覆盖此处的设置)
  11. header: { // 导航栏相关设置项
  12. backTitle: '返回',// 左上角返回键文字
  13. style: {
  14. backgroundColor: '#fff'
  15. },titleStyle: {
  16. color: 'green'
  17. }
  18. },cardStack: {
  19. gesturesEnabled: true
  20. }
  21. },mode: 'card',// 页面切换模式,左右是card(相当于iOS中的push效果),上下是modal(相当于iOS中的modal效果)
  22. //让安卓实现push动画
  23. headerMode: 'screen',transitionConfig:()=>({
  24. screenInterpolator:CardStackStyleInterpolator.forHorizontal,})
  25. onTransitionStart: ()=>{ console.log('导航栏切换开始'); },// 回调
  26. onTransitionEnd: ()=>{ console.log('导航栏切换结束'); } // 回调
  27. });

  1. //页面跳转传参
  2. navigate('Detail',{
  3. title:'图片详情',url:item.url,// 跳转的时候携带一个参数去下个页面
  4. callback: (data)=>{
  5. console.log(data); // 打印值为:'回调参数'
  6. }
  7. });
  8. const {navigate,goBack,state} = this.props.navigation;
  9. // 在第二个页面,在goBack之前,将上个页面方法取到,并回传参数,这样回传的参数会重走render方法
  10. state.params.callback('回调参数');
  11. goBack();
  12.  
  13. //通过static navaigationOptions来初始化页面
  14. //属性给params
  15. componentDidMount(){
  16. this.props.navigation.setParams({
  17. title:'自定义Header',navigatePress:this.navigatePress
  18. })
  19. }
  20. navigatePress = () => {
  21. alert('点击headerRight');
  22. console.log(this.props.navigation);
  23. }
  24. //接下来就可以通过params方法获取点击事件了
  25. static navigationOptions = ({ navigation,screenProps }) => ({
  26. title: navigation.state.params.title,headerRight:(
  27. <Text onPress={navigation.state.params.navigatePress}>
  28. 返回
  29. </Text>
  30. )
  31. });

demo1:

  1. import React from 'react';
  2. import {
  3. AppRegistry,Text,Button,View,} from 'react-native';
  4. import { StackNavigator } from 'react-navigation';
  5. class HomeScreen extends React.Component {
  6. static navigationOptions = {
  7. title: 'Welcome',};
  8. render() {
  9. const { navigate } = this.props.navigation;
  10. return (
  11. <View>
  12. <Button
  13. onPress={() => navigate('Chat',{user: 'xh'})}
  14. title="Chat with xh"
  15. />
  16. </View>
  17. );
  18. }
  19. }
  20. class ChatScreen extends React.Component {
  21. static navigationOptions = ({ navigation }) => ({
  22. title: `Chat with ${navigation.state.params.user}`,});
  23. render() {
  24. const { params } = this.props.navigation.state;
  25. return (
  26. <View>
  27. <Text>Chat with {params.user}</Text>
  28. </View>
  29. );
  30. }
  31. }
  32. const ReactNavigation = StackNavigator({
  33. Home: { screen: HomeScreen },Chat: { screen: ChatScreen },});

TabNavigator 标签

  1. const MyTab = TabNavigator({
  2. Home: {
  3. screen: Home,navigationOptions:{
  4. tabBarLabel: '首页',tabBarIcon: ({tintColor}) => (
  5. <Image
  6. source={{uri : ''}}
  7. style={[tabBarIcon,{tintColor: tintColor}]}
  8. />
  9. ),},{
  10. tabBarPosition: 'bottom',swipeEnabled:false,animationEnabled:false,tabBarOptions: {
  11. style: {
  12. height:49
  13. },activeBackgroundColor:'white',activeTintColor:'#4ECBFC',inactiveBackgroundColor:'white',inactiveTintColor:'#aaa',showLabel:false,}
  14. });

demo2:

  1. import React from 'react';
  2. import {
  3. AppRegistry,} from 'react-native';
  4. import { StackNavigator } from 'react-navigation';
  5. import { TabNavigator } from 'react-navigation';
  6. class ChatScreen extends React.Component {
  7. static navigationOptions = ({ navigation }) => ({
  8. title: `Chat with ${navigation.state.params.user}`,});
  9. render() {
  10. const { params } = this.props.navigation.state;
  11. return (
  12. <View>
  13. <Text>Chat with {params.user}</Text>
  14. </View>
  15. );
  16. }
  17. }
  18. class Home1Screen extends React.Component {
  19. render() {
  20. const { navigate } = this.props.navigation;
  21. return (
  22. <View>
  23. <Button
  24. onPress={() => navigate('Chat',{user: 'user1'})}
  25. title="Chat with user1"
  26. />
  27. </View>
  28. );
  29. }
  30. }
  31. class Home2Screen extends React.Component {
  32. render() {
  33. const { navigate } = this.props.navigation;
  34. return (
  35. <View>
  36. <Button
  37. onPress={() => navigate('Chat',{user: 'user2'})}
  38. title="Chat with user2"
  39. />
  40. </View>
  41. );
  42. }
  43. }
  44. const ScreenNavigator = TabNavigator({
  45. Home1: { screen: Home1Screen },Home2: { screen: Home2Screen },});
  46. ScreenNavigator.navigationOptions = {
  47. title: 'index title',};
  48. const ReactNavigation = StackNavigator({
  49. Home: { screen: ScreenNavigator },});
  50.  
  51. //增加一个右边按钮
  52. static navigationOptions = ({ navigation }) => {
  53. const {state,setParams} = navigation;
  54. const isInfo = state.params.mode === 'info';
  55. const {user} = state.params;
  56. return {
  57. title: isInfo ? `${user}'s Contact Info` : `Chat with ${state.params.user}`,headerRight: (
  58. <Button
  59. title={isInfo ? 'Done' : `${user}'s info`}
  60. onPress={() => setParams({ mode: isInfo ? 'none' : 'info'})}
  61. />
  62. ),};
  63. };

猜你在找的React相关文章