javascript - 将用户数据从一个屏幕传递到另一个屏幕 - React Native

标签 javascript firebase react-native google-cloud-firestore firebase-authentication

我是 React Native 的新手,我正在尝试将数据 user.uid 从 LoginAdult 屏幕推送并获取(并打印)到 HomeParent 屏幕。在 HomeParent 屏幕中,我想使用它(将其传递到其他屏幕)。如果有人帮助我,我将不胜感激!

登录成人.js:

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 中执行以下操作,

 constructor(props){
    super(props);

    const user = this.props.navigation.getParam('user');
    this.state={
    user,
    }

但理想情况下,这不是标准方法,您可以做的是将 userId 保存在 AsyncStorage 中,例如 AsyncStorage.setItem('userId'); 并获取 userId,您可以在 componentDidMount 中创建一个异步函数,例如

componentDidMount(){
this.handleData();
}

handleData=async()=>{
const userId = await AsyncStorage.getItem('userId');
}

关于javascript - 将用户数据从一个屏幕传递到另一个屏幕 - React Native,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66572680/

相关文章:

java - Worklight : how to set use addGlobalHeader for WL. Client.invokeProcedure 调用

react-native - 解析 React Native 中导入的 src 目录

node.js - 在 React Native 中处理加密

firebase - Firestore:合并2个流并同时发射两个值

android - 更改 Firebase 包名称

java - Gradle 同步失败 : A problem occurred evaluating settings

javascript - Sequelize CLI 在执行使用 db : migrate 创建表的命令时返回错误

javascript - video.js grunt 意外的标识符

javascript - ag-grid:引用节点的层次树:如何使引用的属性可编辑?

java - 如何通过其uid获取Firestore用户列表?