Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何获取Firebase用户对象并在不同的组件中使用它?_Reactjs_Firebase_Firebase Authentication - Fatal编程技术网

Reactjs 如何获取Firebase用户对象并在不同的组件中使用它?

Reactjs 如何获取Firebase用户对象并在不同的组件中使用它?,reactjs,firebase,firebase-authentication,Reactjs,Firebase,Firebase Authentication,我正在使用Firebase并尝试获取当前登录用户。我检查用户是否使用componentDidMount登录,如果用户已登录,我会尝试更新我的状态 现在,即使我的控制台中有一个Firebase用户对象,状态也永远不会更新。首次使用toggleSignIn方法注册用户时,它也不会更新 所有这些都是在我的App.js文件中完成的。我想知道是否有更好的方法来获取当前登录的用户并相应地更新状态,这样我就可以在其他组件中使用用户对象,并获取诸如用户photoURL,displayName等内容 var Ap

我正在使用Firebase并尝试获取当前登录用户。我检查用户是否使用
componentDidMount
登录,如果用户已登录,我会尝试更新我的状态

现在,即使我的控制台中有一个Firebase用户对象,状态也永远不会更新。首次使用
toggleSignIn
方法注册用户时,它也不会更新

所有这些都是在我的
App.js文件中完成的。我想知道是否有更好的方法来获取当前登录的用户并相应地更新状态,这样我就可以在其他组件中使用用户对象,并获取诸如用户
photoURL
displayName
等内容

var App = React.createClass({
  **getInitialState:** function () {
    // Set initial state of user to null
    return {
      user : null
    }
  },
  componentDidMount: function () {
    // Checks to see if a user is signed in and sets state to the user object.
    firebase.auth().onAuthStateChanged(function(user) {
      if (user) {
        **this.setState({
          user: user
        });**
      } else {
        // No user is signed in.
      }
    });
  },
  toggleSignIn: function () {
    if (!firebase.auth().currentUser) {
      // Create an instance of the Google provider object
      var provider = new firebase.auth.GoogleAuthProvider();
      // Sign in with popup.
      firebase.auth().signInWithPopup(provider).then(function(result) {
        var token = result.credential.accessToken;
        // Get signed-in user info
        **this.setState({ 
            user: result.user
        });**
        // ...
      }).catch(function(error) {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
        // The email of the user's account used.
        var email = error.email;
        // The firebase.auth.AuthCredential type that was used.
        var credential = error.credential;
        // ...
      });
    } else {
      // Sign user out
      firebase.auth().signOut();
    }
  },
  render: function () {
    return (
      <div className="app">
        <main  className="app-content">
          {this.props.children}
        </main>
      </div>
    );
  }
});

module.exports = App;
var-App=React.createClass({
**getInitialState:*函数(){
//将用户的初始状态设置为null
返回{
用户:空
}
},
componentDidMount:函数(){
//检查用户是否已登录,并为用户对象设置状态。
firebase.auth().onAuthStateChanged(函数(用户){
如果(用户){
**这是我的国家({
用户:用户
});**
}否则{
//没有用户登录。
}
});
},
toggleSignIn:函数(){
如果(!firebase.auth().currentUser){
//创建Google provider对象的实例
var provider=new firebase.auth.GoogleAuthProvider();
//使用弹出窗口登录。
firebase.auth().signInWithPopup(提供程序)。然后(函数(结果){
var token=result.credential.accessToken;
//获取登录用户信息
**这个.setState({
用户:result.user
});**
// ...
}).catch(函数(错误){
//在这里处理错误。
var errorCode=error.code;
var errorMessage=error.message;
//使用的用户帐户的电子邮件。
var email=error.email;
//使用的firebase.auth.AuthCredential类型。
var-credential=error.credential;
// ...
});
}否则{
//注销用户
firebase.auth().signOut();
}
},
渲染:函数(){
返回(
{this.props.children}
);
}
});
module.exports=App;