React Native:使用堆栈导航器触摸图像后的新屏幕

时间:2019-04-18 09:37:01

标签: react-native expo stack-navigator onpress touchablewithoutfeedback

我正在尝试通过创建一个应用来学习React Native。触摸图像后,我希望将新屏幕放置在当前屏幕的顶部。

我认为我想在StackNavigator之后使用onPress吗?我已经阅读了有关它们的内容,但不知道如何在当前代码中使用它们。有什么想法吗?

const listView = ({name, picture}) => { 
       return (  
        <View style = {styles.item}>  
          <TouchableWithoutFeedback onPress={() => //new screen here!!}>  
            <ImageBackground source = {picture}  
                             resizeMode = 'cover'  
                             style={styles.image}>  
                <Text style={styles.text}>{name}</Text>  
            </ImageBackground>  
            </TouchableWithoutFeedback>  
        </View>  
      )  
};

请在评论中留下您需要的任何问题。

1 个答案:

答案 0 :(得分:0)

您可以使用Createrestacknavigator.

exampleApp.js:

import React, { Component } from "react";
import {
  createStackNavigator,
  createAppContainer,
} from "react-navigation";
import Home from "./Home.js"
import Second from "./Second.js"
...
const RootStack = createStackNavigator(
  {
    Home: {
      screen: Home
    },
    Second: {
      screen: Second
    }   
  },
  {
    initialRouteName: "Home",
    headerMode: "none"
  }
);
const AppContainer = createAppContainer(RootStack);
export default AppContainer;

exampleHome.js:

 return (  
        <View style = {styles.item}>  
          <TouchableOpacity onPress={() => this.props.navigation.navigate("Second")}>  
            <ImageBackground source = {picture}  
                             resizeMode = 'cover'  
                             style={styles.image}>  
                <Text style={styles.text}>{name}</Text>  
            </ImageBackground>  
            </TouchableOpacity >  
        </View>  
      )  

有关功能的更多信息,请参见this link