如何制作 React 原生路由

How to make React native route

所以我是 React Native 的新手,所以我开始了 Udemy 课程来学习它。该课程有点旧,所以我不确定代码是否已过时,但重点是我试图学习如何在一个简单的应用程序中进行路由以更改屏幕,但是当我单击按钮时,没有任何反应。 App.js:

    import React from 'react';
import { StyleSheet, Text, View, Platform, Image, ImageBackground } from 'react-native';
import {Button} from 'native-base';
import { render } from 'react-dom';
import Landing from './src/Landing';
import Search from './src/search';

var myBackground = require('./assets/icons/landing.jpg');

export default class App extends React.Component {
  state = {
    currentScreen: "landing"
  }
  switchScreen = (currentScreen) => {
    this.setState({currentScreen});
  }
  renderScreen = () =>{
    if(this.state.currentScreen === "landing") {
      return (
        <Landing switchScreen={this.switchScreen }/>
      )
    }
    else if(this.state.currentScreen === "search") {
      return(
        <Search/>
      )
    }
  }
  render() {
  return (
    <View style={styles.container}>
      {this.renderScreen()}
    </View>
  );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    marginTop: Platform.OS === "android" ? 50 : 0
  },
});

登陆组件:

import React from 'react';
import {View, Text, Image, StyleSheet, ImageBackground} from 'react-native';
import {Button} from 'native-base';

var myBackground = require('../assets/icons/landing.jpg');

class Landing extends React.Component {
    render() {
        return(
            <View>
                <ImageBackground style={ styles.imgBackground } source={myBackground}>
                    <View style={styles.viewStyle}>
                    <Text style={styles.titleStyle}>Welcome to PokeSearch</Text>
                    <Button block={true} style={styles.buttonStyle} onPress={()=>this.props.switchScreen("screen")}>
                        <Text style={styles.buttonText}>Start Searching for Pokemon!</Text>
                    </Button>
                    </View>
                </ImageBackground>
            </View>
        )
    }
}

const styles = StyleSheet.create({
    container: {
      flex: 1,
      marginTop: Platform.OS === "android" ? 50 : 0
    },
    imgBackground: {
      width: '100%',
      height: '100%',
      flex: 1 
  },
  viewStyle: {
    flex: 1,
    flexDirection: 'column',
    justifyContent: "center",
    alignItems: "center",
    marginTop: 50,
  },
  titleStyle: {
    fontSize: 30,
    color: 'red',
    alignItems: 'center',
  },
  buttonStyle: {
    margin: 10
  },
  buttonText: {
    color: 'red'
  },
  });


export default Landing;

并且搜索非常简单:

import React from 'react';
import {View,Text} from 'react-native';

class Search extends React.Component {
    render() {
        return (
            <View>
                <Text>Search</Text>
            </View>
        )
    }
}

export default Search;

因此,当我单击该按钮时,它应该会切换到搜索页面,但什么也没有发生。我还注意到该按钮在单击时不会执行应有的 Flash 动画。有没有人对如何使这项工作有任何建议,请提前致谢!

您可以按照此 link.

构建导航

https://reactnavigation.org/docs/en/getting-started.html

此处是典型的导航实现。

https://github.com/apiko-dev/Perfi/tree/master/app/navigation