如何制作React本机路由

问题描述 投票:0回答:1

所以我是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动画。有没有人对如何使此工作有任何建议,请先感谢!

javascript reactjs react-native routing
1个回答
0
投票

您可以通过单击此链接来构建导航。

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

导航的典型实现在这里。

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

如果有帮助,请投票。最好,

© www.soinside.com 2019 - 2024. All rights reserved.