react-native 的页面跳转

版权声明:我写的你不能转载,但是你可以复制啊。复制记得加关注啊(迷之微笑)。 https://blog.csdn.net/quhongqiang/article/details/86598699

你需要先执行:

yarn add react-navigation
# or with npm
# npm install --save react-navigation

然后,安装 react-native-gesture-handler。 如果你使用 Expo,就什么都不需要做,他已经包含在 SDK 中 了, 否则:

yarn add react-native-gesture-handler
# or with npm
# npm install --save react-native-gesture-handler

先上一张目录图!

用到的只有src下面的2个文件和App.js   后面我贴代码

1、MyPage.js

import React, {Component} from 'react';
import { View, Text, Button } from 'react-native';
import { StackActions, NavigationActions } from 'react-navigation';

export default class DetailsScreen extends Component {
  render() {
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text>Details222222222222</Text>
      </View>
    );
  }
}

2、HomePage.js

import React, {Component} from 'react';
import { View, Text, Button } from 'react-native';
import { StackActions, NavigationActions } from 'react-navigation'; 

export default class HomeScreen extends Component {
  render() {
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text>Home Screen</Text>
        <Button
          title="Go to Details"
          onPress={() => {
            this.props.navigation.dispatch(StackActions.reset({
              index: 0,
              actions: [
                NavigationActions.navigate({ routeName: 'Details' })
              ],
            }))
          }}
        />
      </View>
    );
  }
}

3、App.js

import React from 'react';
import { View, Text, Button } from 'react-native';
import { createAppContainer, createStackNavigator, StackActions, NavigationActions } from 'react-navigation';
import HomeScreen from "./src/pages/home/HomePage";
import DetailsScreen from "./src/pages/my/MyPage";

const AppNavigator = createStackNavigator({
  Home: {
    screen: HomeScreen,
  },
  Details: {
    screen: DetailsScreen,
  },
}, {
    initialRouteName: 'Home',
});

export default createAppContainer(AppNavigator);

就可以运行了,如果遇到了undefined is not an object (evaluating 'RNGestureHandlerModule.State'

请转往此处:https://blog.csdn.net/quhongqiang/article/details/86597694

采坑之路  欢迎讨论。!~~

猜你喜欢

转载自blog.csdn.net/quhongqiang/article/details/86598699