简体   繁体   English

React函数不返回任何值

[英]React function not returning any value

I'm currently using firebase to populate a website with a certain user's first and last name, but for some reason it isn't working. 我目前正在使用Firebase用某个用户的名字和姓氏填充网站,但是由于某种原因,它无法正常工作。

getUserName(){
    firebaseAuth().onAuthStateChanged((user) => {
        if(user){
            // var name = ref.ref('users/' + user.uid).once('value').then((snapshot) => console.log(snapshot.val().displayName));
            var name = user.displayName;
            console.log("name in function: " + name); //prints full name
            return name;
        }
    });
}


render() {
    var name = this.getUserName();
    console.log("name: " + name); //prints undefined
    return (
        <div className="col-sm-6 col-sm-offset-3">
            <h1> Welcome {this.getUserName()} asdfasd </h1>
        </div>
    );
}

These are my two functions. 这是我的两个功能。 For some reason, when I print out the name variable in both cases, I get two different variables. 由于某种原因,在两种情况下我都打印出名称变量时,会得到两个不同的变量。 In getUserName(), it prints out the user's full name, and I then return that value. 在getUserName()中,它打印出用户的全名,然后返回该值。 When the DOM renders, the "name" variable is undefined even though the getUserName() function prints out the full name. 当DOM呈现时,即使getUserName()函数打印出全名,“名称”变量也是未定义的。 Any help would be greatly appreciated! 任何帮助将不胜感激!

Your method getUserName() doesn't actually return anything, so when you try to assign its return value to the name -variable, like this var name = this.getUserName(); 您的方法getUserName()实际上不返回任何内容,因此,当您尝试将其返回值分配给name -variable时,例如var name = this.getUserName(); , you'll always just set name to undefined . ,您总是将name设置为undefined

As Shailesh Prajapati suggests, you should set the name in the component state. 正如Shailesh Prajapati所建议的那样,您应该在组件状态下设置名称。 I would even argue, you should separate it out to a global state using something like Redux. 我什至会争论,您应该使用Redux之类将其分离到一个全局状态。 Also, you shouldn't call this.getUserName() within the render() function, as it could (and most likely will) be very bad for performance. 另外,您不应该在render()函数中调用this.getUserName() ,因为它可能(并且很可能会)对性能造成严重影响。

Here's the code, with everything suggested: 这是代码,其中包含所有建议内容:

componentWillMount(){
    this.getUserName();
}

getUserName(){
    firebaseAuth().onAuthStateChanged((user) => {
        if(user){
            var name = user.displayName;
            this.setState({ name: name });
        }
    });
}

render() {
    return (
        <div className="col-sm-6 col-sm-offset-3">
            <h1> Welcome {this.state.name} asdfasd </h1>
        </div>
    );
}

You Have to use State instead of variable.Store name value in this.SetState({name: value}) and use directly using this.state.name where ever you want to use... 您必须使用状态而不是变量。将名称值存储在this.SetState({name: value})并在要使用的this.state.name地方直接使用this.state.name ...

Local variable cannot access from outside function so you have to use state in react to access state/variable outside the function.. 局部变量无法从外部函数访问,因此您必须使用状态来对函数外部的访问状态/变量作出反应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM