TypeError:未定义不是对象(评估' props.route')
一直在尝试在我的React应用程序上制作两个不同的用户,但是我使用道具遇到问题来连接屏幕。当我按“没有帐户”时,我会遇到错误。是否有其他没有道具的方法来执行此操作,或者是否有任何解决方案? 感谢任何建议。 谢谢
Inigupscreen
import React,{useState, useEffect} from 'react'
import { View, Text ,Image,StyleSheet,KeyboardAvoidingView,TouchableOpacity,ActivityIndicator,ScrollView} from 'react-native'
import { TextInput,Button } from 'react-native-paper';
import {launchImageLibrary} from 'react-native-image-picker';
import storage from '@react-native-firebase/storage'
import auth from '@react-native-firebase/auth'
import firestore from '@react-native-firebase/firestore'
const SignupScreen =({navigation,route,props}) =>{
const [email,setEmail] = useState('')
const [name,setName] = useState('')
const [password,setPassword] = useState('')
const [image,setImage] = useState(null)
const [showNext,setShowNext] = useState(false)
const [loading,setLoading] = useState(false)
if(loading){
return <ActivityIndicator size="large" color="#00ff00" />
}
const userSignup = async ()=>{
setLoading(true)
if(!email || !password || !name){
alert("please add all the field")
return
}
try{
const result = await auth().createUserWithEmailAndPassword(email,password)
firestore().collection('users').doc(result.user.uid).set({
name:name,
email:result.user.email,
uid:result.user.uid,
pic:image,
status:"online"
})
setLoading(false)
}catch(err){
alert("something went wrong")
}
}
const type=props.route.params.type
// console.log(type,"type");
useEffect(()=>{
allUsers()
},[])
if(type=='Doctor'){
return (
<ScrollView>
<KeyboardAvoidingView behavior="position">
<View style={styles.box1}>
<Image style={styles.img} source={require('../assets/hee.png')} />
</View>
<View style={styles.box2}>
{!showNext &&
<>
<TextInput
label="Email"
value={email}
onChangeText={(text)=>setEmail(text)}
mode="outlined"
/>
<TextInput
label="password"
mode="outlined"
value={password}
onChangeText={(text)=>setPassword(text)}
secureTextEntry
/>
<TextInput
label="Full Name"
mode="outlined"
/>
<TextInput
label="Address"
mode="outlined"
/>
<TextInput
label="Contact Number"
mode="outlined"
keyboardType='numeric'
/>
</>
}
{showNext ?
<>
<TextInput
label="Name"
value={name}
onChangeText={(text)=>setName(text)}
mode="outlined"
/>
<Button
mode="contained"
style={{marginTop:20}}
onPress={()=>userSignup()}
>Signup</Button>
<TouchableOpacity style={{textAlign:"center", marginTop:25,marginLeft:115}} onPress={()=>navigation.goBack()}>
<Text>
Go Back
</Text>
</TouchableOpacity>
</>
:
<Button
style={{marginTop:20}}
mode="contained"
onPress={()=>setShowNext(true)}
>Next</Button>
}
<TouchableOpacity onPress={()=>props.navigation.goBack()}><Text style={{textAlign:"center", marginTop:25}}>Already have an account ?</Text></TouchableOpacity>
</View>
</KeyboardAvoidingView>
</ScrollView>
)
}
}
export default SignupScreen
const styles = StyleSheet.create({
text:{
fontSize:22,
color:"green",
margin:15
},
img:{
width:250,
height:250
},
box1:{
alignItems:"center"
},
box2:{
width:"70%",
justifyContent:"center",
height:"50%",
margin:20,
marginLeft:55
}
});
Main
import React,{useState} from 'react'
import { View, Text ,Image,StyleSheet,KeyboardAvoidingView,TouchableOpacity,ActivityIndicator} from 'react-native'
import { TextInput,Button } from 'react-native-paper';
import auth from '@react-native-firebase/auth'
const Main =props=> {
const [email,setEmail] = useState('')
const [password,setPassword] = useState('')
const [loading,setLoading] = useState(false)
if(loading){
return <ActivityIndicator size="large" color="red" />
}
const userLogin = async ()=>{
setLoading(true)
if(!email || !password){
alert("please add all the field")
return
}
try{
const result = await auth().signInWithEmailAndPassword(email,password)
setLoading(false)
}catch(err){
alert("something went wrong")
}
}
return (
<KeyboardAvoidingView behavior="position">
<View style={styles.box1}>
<Image style={styles.img} source={require('../assets/hee.png')} />
</View>
<View style={styles.box2}>
<Button
mode="contained"
onPress={()=>{userLogin(),{type:"Doctor"}}}
>Doctor</Button>
<Button
mode="contained"
onPress={()=>{userLogin(),{type:"User"}}}
>User</Button>
<TouchableOpacity
onPress={()=>props.navigation.navigate('signup')}>
<Text style={{textAlign:"center"}}>
Dont have an account ?
</Text>
</TouchableOpacity>
</View>
</KeyboardAvoidingView>
)
}
export default Main
const styles = StyleSheet.create({
text:{
fontSize:22,
color:"red",
margin:10
},
img:{
width:250,
height:250
},
box1:{
alignItems:"center"
},
box2:{
paddingHorizontal:40,
justifyContent:"space-evenly",
height:"50%"
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
props
没有定义,因为您已经破坏了它。因此,您可以简单地设置类型
变量:props
is not defined because you already destructed it. So, you can set thetype
variable simply like this:正如 @user456所建议的那样,您不需要使用
props.route
,因为您已经在破坏了道具。在您的
Inbeupscreen
还会在您的
Main
文件中更改当时,您将必须传递
type
param当您导航到signupscreen
。更改
没有帐户
按钮onpress您可以阅读有关在 https://reactnavigation.org/docs/params
As @User456 suggested, you don't need to use
props.route
as you are already destructuring the props.In your
SignupScreen
Also change the
Then in your
Main
file, you will have to pass thetype
param when you navigate to theSignupScreen
.Change the
Don't have an account
button onPress toYou can read more about passing route params in react-navigation at https://reactnavigation.org/docs/params