【问题标题】:React Native Button navigate to another pageReact Native 按钮导航到另一个页面
【发布时间】:2021-11-18 18:48:56
【问题描述】:

我是 React Native 的新手。我需要如果我点击一个按钮,就会出现一个新页面。我已经尝试了一些事情,但也许问题出在其他地方。 我将在下面粘贴我的代码。请帮帮我:)

进一步说明:我想点击按钮,然后它应该转到另一个页面。

import React, { useState } from "react";
import {
  StyleSheet,
  Text,
  View,
  Image,
  TextInput,
  TouchableOpacity,
  Linking,
  Button
} from "react-native";   
import { useNavigation } from '@react-navigation/native';
import "react-native-gesture-handler";
import { NavigationContainer } from '@react-navigation/native'; 
import { createStackNavigator, createAppContainer } from 'react-navigation';  




export default function App() {
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");


  return (
    <View style={styles.container}>
      <Image style={styles.image} source={require("./assets/logo.png")} />

      <StatusBar style="auto" />
      <View style={styles.inputView}>
        <TextInput
          style={styles.TextInput}
          placeholder="Benutzername"
          onChangeText={(email) => setEmail(email)}
        />
      </View>

      <Button  
          style={styles.loginBtn}
          title="Go to Profile"  
          onPress={() => this.props.navigation.navigate('Profile')}  
      />  
    </View>
  );
}
```

【问题讨论】:

  • 您在设置应用程序时是否遵循 react-navigation 文档?您必须将您的应用程序包装在应用程序容器中并创建一个包含两个屏幕的堆栈。如果您在其他地方做过,请包含该代码。否则,您可以参考the docs 来设置您的项目
  • 你遇到了什么问题?
  • 您的整个实现是错误的,您的一些导入来自 react 本机导航版本 5 和 4 --- 请阅读更多信息并关注它reactnavigation.org/docs/getting-started

标签: javascript react-native button onpress


【解决方案1】:

可能对你有帮助:

我的导航版本

@react-navigation/native": "^5.9.8"   
@react-navigation/stack": "^5.14.9"

示例

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

//Navigation import
import {NavigationContainer} from '@react-navigation/native';
import {createStackNavigator} from '@react-navigation/stack';

//Screen One
const ScreenOne = props => {

  //onPress To Navigate
  const onPress = () => {
    props.navigation.navigate('ScreenTwo');
  };

  return (
    <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
      <TouchableOpacity onPress={onPress}>
        <Text>Hello From Screen One</Text>
      </TouchableOpacity>
    </View>
  );
};

//Screen Two
const ScreenTwo = () => {
  return (
    <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
      <Text>Screen Two</Text>
    </View>
  );
};

const App = () => {
  //const
  const Stack = createStackNavigator();

  return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen name="ScreenOne" component={ScreenOne} />
        <Stack.Screen name="ScreenTwo" component={ScreenTwo} />
      </Stack.Navigator>
    </NavigationContainer>
  );
};

export default App;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-08
    • 1970-01-01
    • 2021-08-19
    • 2010-10-08
    • 1970-01-01
    • 1970-01-01
    • 2014-07-01
    • 1970-01-01
    相关资源
    最近更新 更多