无法在React Native中浏览屏幕

发布于 2025-02-13 06:04:12 字数 4265 浏览 0 评论 0原文

我试图在按钮上浏览越过不同的屏幕,请在React Native中单击。以下是代码:

app.tsx

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

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

import Login from './src/screens/Login/login'
import SignUp from './src/screens/SignUp/signUp'
import { getItem } from './src/utility/AsyncStorage'

export default function App() {
  const [gmailId, setGmailId] = React.useState("");
  const [password, setPassword] = React.useState("");
  React.useEffect(() => {
    getItem("gmailId").then((value:any) => {setGmailId(value)});
    getItem("password").then((value:any) => {setPassword(value)});
  }, []);

  const RootStack = createNativeStackNavigator();

  return (
    <View style={styles.container}>
      <NavigationContainer>
        <RootStack.Navigator initialRouteName="Login">
          <RootStack.Screen name="Login" component={Login} />
          <RootStack.Screen name="signup" component={SignUp} />
        </RootStack.Navigator>
      </NavigationContainer>
        {/* {(!gmailId && !password)? <Login/>: <Text>Logged In</Text>} */}
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  }
});

login.tsx

import React from "react";
import { Text, View, Image, TextInput, TouchableOpacity} from 'react-native';
import { SocialIcon } from 'react-native-elements'


import { loginStyle } from './loginStyle'
import { signIn } from './loginLogic'


const Login = (navigation:any) => {
  const [gmailId, setGmailId] = React.useState("");
  const [password, setPassword] = React.useState("");

  const navigateToSignUp = () => {
    navigation.navigate('signup')
  };

  return (
    <View style={styles.container}>
      <Image
        style={styles.tinyLogo}
        source={require( "../../../assets/icon.png")}
      />
      <View style={{ width: '100%', alignItems: 'center', justifyContent: 'center', marginTop: 30}}>
        <TextInput
          style={styles.input}
          placeholder="Enter Gmail Id"
          onChangeText={setGmailId}
        />
        <TextInput
          style={styles.input}
          onChangeText={setPassword}
          placeholder="Enter your Password"
          secureTextEntry={true}
        />
        <TouchableOpacity
          style={[styles.button, styles.loginButton]}
          onPress={()=>signIn(gmailId, password)}>
            <Text style={styles.text}>Login</Text>
        </TouchableOpacity>
        <TouchableOpacity
          style={[styles.button, styles.signupButton]}
          onPress={() => navigateToSignUp}>
            <Text style={styles.text}>Create New Account</Text>
        </TouchableOpacity>
      </View>
      <View style={{flexDirection: 'row', alignItems: 'center', width: '95%', margin: 10, marginTop: 25}}>
        <View style={{flex: 1, height: 1, backgroundColor: 'black'}} />
        <View>
          <Text style={{width: 100, textAlign: 'center'}}>Social Login</Text>
        </View>
        <View style={{flex: 1, height: 1, backgroundColor: 'black'}} />
      </View>
      <SocialIcon
        style={{width:"85%"}}
        title='Sign In With Google'
        button
        type='google'
        onPress={()=>{console.log('Sign In With Google')}}
      />
    </View>
  )
}

export default Login

//===================== STYLE SECTION =====================
const styles = loginStyle()

ignup.tsx

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

const SignUp = () => {
  return (
    <View>
      <Text>signUp</Text>
    </View>
  )
}

export default SignUp

const styles = StyleSheet.create({})

我的编码没有丢弃任何错误,但都看不到任何屏幕(甚至都不是初始路线)。运行代码时,它仅显示一个白页。

请帮助我纠正这个问题。

I was trying to navigate cross different screens on button click in React Native. Following is the code:

App.tsx

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

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

import Login from './src/screens/Login/login'
import SignUp from './src/screens/SignUp/signUp'
import { getItem } from './src/utility/AsyncStorage'

export default function App() {
  const [gmailId, setGmailId] = React.useState("");
  const [password, setPassword] = React.useState("");
  React.useEffect(() => {
    getItem("gmailId").then((value:any) => {setGmailId(value)});
    getItem("password").then((value:any) => {setPassword(value)});
  }, []);

  const RootStack = createNativeStackNavigator();

  return (
    <View style={styles.container}>
      <NavigationContainer>
        <RootStack.Navigator initialRouteName="Login">
          <RootStack.Screen name="Login" component={Login} />
          <RootStack.Screen name="signup" component={SignUp} />
        </RootStack.Navigator>
      </NavigationContainer>
        {/* {(!gmailId && !password)? <Login/>: <Text>Logged In</Text>} */}
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  }
});

login.tsx

import React from "react";
import { Text, View, Image, TextInput, TouchableOpacity} from 'react-native';
import { SocialIcon } from 'react-native-elements'


import { loginStyle } from './loginStyle'
import { signIn } from './loginLogic'


const Login = (navigation:any) => {
  const [gmailId, setGmailId] = React.useState("");
  const [password, setPassword] = React.useState("");

  const navigateToSignUp = () => {
    navigation.navigate('signup')
  };

  return (
    <View style={styles.container}>
      <Image
        style={styles.tinyLogo}
        source={require( "../../../assets/icon.png")}
      />
      <View style={{ width: '100%', alignItems: 'center', justifyContent: 'center', marginTop: 30}}>
        <TextInput
          style={styles.input}
          placeholder="Enter Gmail Id"
          onChangeText={setGmailId}
        />
        <TextInput
          style={styles.input}
          onChangeText={setPassword}
          placeholder="Enter your Password"
          secureTextEntry={true}
        />
        <TouchableOpacity
          style={[styles.button, styles.loginButton]}
          onPress={()=>signIn(gmailId, password)}>
            <Text style={styles.text}>Login</Text>
        </TouchableOpacity>
        <TouchableOpacity
          style={[styles.button, styles.signupButton]}
          onPress={() => navigateToSignUp}>
            <Text style={styles.text}>Create New Account</Text>
        </TouchableOpacity>
      </View>
      <View style={{flexDirection: 'row', alignItems: 'center', width: '95%', margin: 10, marginTop: 25}}>
        <View style={{flex: 1, height: 1, backgroundColor: 'black'}} />
        <View>
          <Text style={{width: 100, textAlign: 'center'}}>Social Login</Text>
        </View>
        <View style={{flex: 1, height: 1, backgroundColor: 'black'}} />
      </View>
      <SocialIcon
        style={{width:"85%"}}
        title='Sign In With Google'
        button
        type='google'
        onPress={()=>{console.log('Sign In With Google')}}
      />
    </View>
  )
}

export default Login

//===================== STYLE SECTION =====================
const styles = loginStyle()

SignUp.tsx

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

const SignUp = () => {
  return (
    <View>
      <Text>signUp</Text>
    </View>
  )
}

export default SignUp

const styles = StyleSheet.create({})

My coding is not throwing any error yet none of the screens are visible (Not even the initialRoute). On running the code, it shows up a White page only.

Please help me rectify the issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最单纯的乌龟 2025-02-20 06:04:12
import React from "react";
import { Text, View, Image, TextInput, TouchableOpacity} from 'react-native';
import { SocialIcon } from 'react-native-elements'


import { loginStyle } from './loginStyle'
import { signIn } from './loginLogic'


const Login = ({navigation:any}) => {
  const [gmailId, setGmailId] = React.useState("");
  const [password, setPassword] = React.useState("");

  const navigateToSignUp = () => {
    navigation.navigate('signup')
  };

  return (
    <View style={styles.container}>
      <Image
        style={styles.tinyLogo}
        source={require( "../../../assets/icon.png")}
      />
      <View style={{ width: '100%', alignItems: 'center', justifyContent: 'center', marginTop: 30}}>
        <TextInput
          style={styles.input}
          placeholder="Enter Gmail Id"
          onChangeText={setGmailId}
        />
        <TextInput
          style={styles.input}
          onChangeText={setPassword}
          placeholder="Enter your Password"
          secureTextEntry={true}
        />
        <TouchableOpacity
          style={[styles.button, styles.loginButton]}
          onPress={()=>signIn(gmailId, password)}>
            <Text style={styles.text}>Login</Text>
        </TouchableOpacity>
        <TouchableOpacity
          style={[styles.button, styles.signupButton]}
          onPress={() => navigateToSignUp}>
            <Text style={styles.text}>Create New Account</Text>
        </TouchableOpacity>
      </View>
      <View style={{flexDirection: 'row', alignItems: 'center', width: '95%', margin: 10, marginTop: 25}}>
        <View style={{flex: 1, height: 1, backgroundColor: 'black'}} />
        <View>
          <Text style={{width: 100, textAlign: 'center'}}>Social Login</Text>
        </View>
        <View style={{flex: 1, height: 1, backgroundColor: 'black'}} />
      </View>
      <SocialIcon
        style={{width:"85%"}}
        title='Sign In With Google'
        button
        type='google'
        onPress={()=>{console.log('Sign In With Google')}}
      />
    </View>
  )
}

export default Login

请尝试。

或者

import React from "react";
import { Text, View, Image, TextInput, TouchableOpacity} from 'react-native';
import { SocialIcon } from 'react-native-elements'


import { loginStyle } from './loginStyle'
import { signIn } from './loginLogic'


const Login = (props:any) => {
  const [gmailId, setGmailId] = React.useState("");
  const [password, setPassword] = React.useState("");

  const navigateToSignUp = () => {
    props.navigation.navigate('signup')
  };

  return (
    <View style={styles.container}>
      <Image
        style={styles.tinyLogo}
        source={require( "../../../assets/icon.png")}
      />
      <View style={{ width: '100%', alignItems: 'center', justifyContent: 'center', marginTop: 30}}>
        <TextInput
          style={styles.input}
          placeholder="Enter Gmail Id"
          onChangeText={setGmailId}
        />
        <TextInput
          style={styles.input}
          onChangeText={setPassword}
          placeholder="Enter your Password"
          secureTextEntry={true}
        />
        <TouchableOpacity
          style={[styles.button, styles.loginButton]}
          onPress={()=>signIn(gmailId, password)}>
            <Text style={styles.text}>Login</Text>
        </TouchableOpacity>
        <TouchableOpacity
          style={[styles.button, styles.signupButton]}
          onPress={() => navigateToSignUp}>
            <Text style={styles.text}>Create New Account</Text>
        </TouchableOpacity>
      </View>
      <View style={{flexDirection: 'row', alignItems: 'center', width: '95%', margin: 10, marginTop: 25}}>
        <View style={{flex: 1, height: 1, backgroundColor: 'black'}} />
        <View>
          <Text style={{width: 100, textAlign: 'center'}}>Social Login</Text>
        </View>
        <View style={{flex: 1, height: 1, backgroundColor: 'black'}} />
      </View>
      <SocialIcon
        style={{width:"85%"}}
        title='Sign In With Google'
        button
        type='google'
        onPress={()=>{console.log('Sign In With Google')}}
      />
    </View>
  )
}

export default Login

这肯定会帮助您

感谢

import React from "react";
import { Text, View, Image, TextInput, TouchableOpacity} from 'react-native';
import { SocialIcon } from 'react-native-elements'


import { loginStyle } from './loginStyle'
import { signIn } from './loginLogic'


const Login = ({navigation:any}) => {
  const [gmailId, setGmailId] = React.useState("");
  const [password, setPassword] = React.useState("");

  const navigateToSignUp = () => {
    navigation.navigate('signup')
  };

  return (
    <View style={styles.container}>
      <Image
        style={styles.tinyLogo}
        source={require( "../../../assets/icon.png")}
      />
      <View style={{ width: '100%', alignItems: 'center', justifyContent: 'center', marginTop: 30}}>
        <TextInput
          style={styles.input}
          placeholder="Enter Gmail Id"
          onChangeText={setGmailId}
        />
        <TextInput
          style={styles.input}
          onChangeText={setPassword}
          placeholder="Enter your Password"
          secureTextEntry={true}
        />
        <TouchableOpacity
          style={[styles.button, styles.loginButton]}
          onPress={()=>signIn(gmailId, password)}>
            <Text style={styles.text}>Login</Text>
        </TouchableOpacity>
        <TouchableOpacity
          style={[styles.button, styles.signupButton]}
          onPress={() => navigateToSignUp}>
            <Text style={styles.text}>Create New Account</Text>
        </TouchableOpacity>
      </View>
      <View style={{flexDirection: 'row', alignItems: 'center', width: '95%', margin: 10, marginTop: 25}}>
        <View style={{flex: 1, height: 1, backgroundColor: 'black'}} />
        <View>
          <Text style={{width: 100, textAlign: 'center'}}>Social Login</Text>
        </View>
        <View style={{flex: 1, height: 1, backgroundColor: 'black'}} />
      </View>
      <SocialIcon
        style={{width:"85%"}}
        title='Sign In With Google'
        button
        type='google'
        onPress={()=>{console.log('Sign In With Google')}}
      />
    </View>
  )
}

export default Login

please try it.

or

import React from "react";
import { Text, View, Image, TextInput, TouchableOpacity} from 'react-native';
import { SocialIcon } from 'react-native-elements'


import { loginStyle } from './loginStyle'
import { signIn } from './loginLogic'


const Login = (props:any) => {
  const [gmailId, setGmailId] = React.useState("");
  const [password, setPassword] = React.useState("");

  const navigateToSignUp = () => {
    props.navigation.navigate('signup')
  };

  return (
    <View style={styles.container}>
      <Image
        style={styles.tinyLogo}
        source={require( "../../../assets/icon.png")}
      />
      <View style={{ width: '100%', alignItems: 'center', justifyContent: 'center', marginTop: 30}}>
        <TextInput
          style={styles.input}
          placeholder="Enter Gmail Id"
          onChangeText={setGmailId}
        />
        <TextInput
          style={styles.input}
          onChangeText={setPassword}
          placeholder="Enter your Password"
          secureTextEntry={true}
        />
        <TouchableOpacity
          style={[styles.button, styles.loginButton]}
          onPress={()=>signIn(gmailId, password)}>
            <Text style={styles.text}>Login</Text>
        </TouchableOpacity>
        <TouchableOpacity
          style={[styles.button, styles.signupButton]}
          onPress={() => navigateToSignUp}>
            <Text style={styles.text}>Create New Account</Text>
        </TouchableOpacity>
      </View>
      <View style={{flexDirection: 'row', alignItems: 'center', width: '95%', margin: 10, marginTop: 25}}>
        <View style={{flex: 1, height: 1, backgroundColor: 'black'}} />
        <View>
          <Text style={{width: 100, textAlign: 'center'}}>Social Login</Text>
        </View>
        <View style={{flex: 1, height: 1, backgroundColor: 'black'}} />
      </View>
      <SocialIcon
        style={{width:"85%"}}
        title='Sign In With Google'
        button
        type='google'
        onPress={()=>{console.log('Sign In With Google')}}
      />
    </View>
  )
}

export default Login

this one surely help you

thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文