React: this is null in event handler

后端 未结 4 1164
-上瘾入骨i
-上瘾入骨i 2020-12-28 12:21

I have a LoginForm component. I want to check before submit, that both loginName and password is set. I tried with this code (a lot of stuff omitte

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-28 13:14

    You need set this for submit method because now this is undefined, for this operation you can use .bind

    onSubmit={ this.submit.bind(this) }
    

    Example

    or you can use arrow function

    onSubmit={ (e) => this.submit(e) }
    

    Example

提交回复
热议问题