Sora
Sora

Reputation: 2551

this.state is always empty string on button click

the following is always returning an empty string on the button click

class Login extends Component {
  constructor(props) {
    super(props);
    this.state = {
      emailAddress: ""
    };
  }
  loaddata = async () => {
    let jiraURL = "";
    let emailres = OTGWindowHelper.GetData();
    let _settingsRes = OfficeClass.GetLocalData("user-credentials");
    let userURLRes = Utils.getUserURL();
    let [userURL, emailData, _settings] = await Promise.all([userURLRes, emailres, _settingsRes]);
    .
    .
    .
    //here i am setting the emailAddress
    this.setState({emailAddress:emailData.EmailAddress})
    // if i add console.log(this.state.emailAddress) i can clearly see the result
   }
   componentDidMount() {
    this.loaddata();
   }
 validateURL = (event) => {
   let userEmailAddress = this.state.emailAddress; // here is always empty

}
render (){
 return (
    <LoginFooter disabled={this.state.disableContinueButton} clicked={this.validateURL} />
 )
}

Upvotes: 0

Views: 137

Answers (1)

Leon
Leon

Reputation: 31

There can be a few reasons why this is happening:

  1. emailData.EmailAddress is empty
  2. you are setting state before the promise has been resolved. In that case, you might want to add a null check before setting the state. Example:
if (emailData.EmailAddress) {
    this.setState({emailAddress:emailData.EmailAddress})
}

Upvotes: 1

Related Questions