reactjs这个上下文在change方法里面改变了怎么调用函数
reactjs this context is changed inside on change method how call function
这是更改,当我调用时,事件侦听器(而不是元素中的 onClick 方法,因为我使用 bootstrap select2)。
getReviewVacancyUpdate(post_id) {
console.log(post_id);
}
componentDidMount() {
$('#post_list').on('change', function(e) {
this.getReviewVacancyUpdate(this.value); //function in undefined
});
}
要不要保存之前的这个状态?也许绑定它?并调用组件方法。没有错误 - “function is undefined
”
我可以在jsx开头加上class
var Child = React.createClass({
statics: {
getReviewVacancyUpdate: function(post_id) {
console.log(post_id);
return true;
}
},
// ...
render() {
}
});
不过看起来不太好
所以我添加了 let 变量来在 on change 方法调用之前保存这个上下文。一切正常。
componentDidMount() {
let save_this = this;
$('#post_list').on('change', function(e){
let selected = $(e.currentTarget).find("option:selected").val();
save_this.getReviewVacancyUpdate(selected);
}).bind(this);
}
这是更改,当我调用时,事件侦听器(而不是元素中的 onClick 方法,因为我使用 bootstrap select2)。
getReviewVacancyUpdate(post_id) {
console.log(post_id);
}
componentDidMount() {
$('#post_list').on('change', function(e) {
this.getReviewVacancyUpdate(this.value); //function in undefined
});
}
要不要保存之前的这个状态?也许绑定它?并调用组件方法。没有错误 - “function is undefined
”
我可以在jsx开头加上class
var Child = React.createClass({
statics: {
getReviewVacancyUpdate: function(post_id) {
console.log(post_id);
return true;
}
},
// ...
render() {
}
});
不过看起来不太好
所以我添加了 let 变量来在 on change 方法调用之前保存这个上下文。一切正常。
componentDidMount() {
let save_this = this;
$('#post_list').on('change', function(e){
let selected = $(e.currentTarget).find("option:selected").val();
save_this.getReviewVacancyUpdate(selected);
}).bind(this);
}