Reputation: 1573
Let's say I have a function :
function onSomethingChanged(field, evt) {
this.validate(field, evt.target.value);
}
And I have a React component where I do :
<input onChanged={this.onSomethingChanged.bind(null, "username")} />
this
is conserved if I pass null
as thisArg ?this
(without writing this.onSomethingChanged.bind(this, "username")
).Thanks !
Upvotes: 0
Views: 57
Reputation: 48487
No, context (this
) of the function will be null
in strict mode
and global object (window
) in non-strict
mode
If you don't like bind
approach you can use ES6 arrow functions to handle this:
<input onChanged={()=>this.onSomethingChanged("username")} />
or if you don't like parenthesis:
<input onChanged={$=>this.onSomethingChanged("username")} />
where $
is event object (can be replaced by e
or any other character at your taste).
Upvotes: 1
Reputation: 7064
No. This is the first argument past into any function. By passing in null you're saying the this is null.
Do this instead.
<input onChanged={this.onSomethingChanged.bind(this, "username")} />
Upvotes: 0