Javascript 将用户数据从一个屏幕传递到另一个屏幕-React Native
我对React Native还不熟悉,我正在尝试将数据user.uid从LoginAdult屏幕推送到(并打印)HomeParent屏幕。在HomeParent屏幕中,我想使用它(将其传递到其他屏幕)。如果有人帮助我,我将不胜感激 LoginAdult.js:Javascript 将用户数据从一个屏幕传递到另一个屏幕-React Native,javascript,firebase,react-native,google-cloud-firestore,firebase-authentication,Javascript,Firebase,React Native,Google Cloud Firestore,Firebase Authentication,我对React Native还不熟悉,我正在尝试将数据user.uid从LoginAdult屏幕推送到(并打印)HomeParent屏幕。在HomeParent屏幕中,我想使用它(将其传递到其他屏幕)。如果有人帮助我,我将不胜感激 LoginAdult.js: class LoginAdult extends React.Component { constructor(props){ super(props) this.state={ email:'',
class LoginAdult extends React.Component {
constructor(props){
super(props)
this.state={
email:'',
password:'',
isLoading:false,
}
}
LoginPress= async()=>{
if(this.state.email&& this.state.password)
{
await firebase.auth().signInWithEmailAndPassword(this.state.email,this.state.password)
.then(
firebase.auth().onAuthStateChanged((user)=>
var docRef=firebase.firestore().collection("Users").doc(user.uid);
docRef.get().then((doc) => {
if (doc.exists) {
console.log("Document data:", doc.data());
const {status} = doc.data();
this.checkStatusLogin(status,{user});
if(status==="Parent")
{
this.props.navigation.navigate("HomeParent",{user});
}
}).catch((error) => {
console.log("Error getting document:", error);
});
}
}))
.catch((error)=>{
}
)
}
}
}
HomeParent.js:
class HomeParent extends React.Component {
constructor(props){
super(props)
this.state={
}
}
componentDidMount(){
const {navigation}=this.props;
const user=navigation.getParam('user');
}
render(){
return(
<View style={styles.logoContainer}>
<Image source={Logo} style={styles.logo}/>
<Text style={{alignItems:'center',justifyContent:'center',margin:40,fontSize:25}}>Home Parent</Text>
</View>
)
}
}
类HomeParent扩展了React.Component{
建造师(道具){
超级(道具)
这个州={
}
}
componentDidMount(){
const{navigation}=this.props;
const user=navigation.getParam('user');
}
render(){
返回(
父母
)
}
}
要传递参数,您需要在HomeParent中执行以下操作:
constructor(props){
super(props);
const user = this.props.navigation.getParam('user');
this.state={
user,
}
但理想情况下,这不是标准方法,您可以做的是将用户标识保存在AsyncStorage中,如AsyncStorage.setItem('userId')
要获取用户ID,可以在componentDidMount中创建一个异步函数,如
componentDidMount(){
this.handleData();
}
handleData=async()=>{
const userId = await AsyncStorage.getItem('userId');
}
函数AsyncStorage.setItem('userId')必须在LoginDefault屏幕中?你能解释一下,我如何使用handleData()函数吗?我如何使用它来打印数据?还有一件事,从user(在您的第一个选项中),我如何仅获取uid?就像user.uid一样,当您直接传递user对象时是的,它将出现在LoginDult屏幕中。创建函数handleData需要考虑的是,可以使代码干净。componentDidMount仅在第一次渲染后工作。因此,此函数将自动调用,您将从asyncstorage获取用户ID。对用户使用Redux不是更好吗?为了在他的整个疗程中使用它?