在react 中.js,将超时引用存储为实例变量(this.timeout)或状态变量(this.state.timeout)更好吗?
React.createClass({
handleEnter: function () {
// Open a new one after a delay
var self = this;
this.timeout = setTimeout(function () {
self.openWidget();
}, DELAY);
},
handleLeave: function () {
// Clear the timeout f或 opening the widget
clearTimeout(this.timeout);
}
...
})
或
React.createClass({
handleEnter: function () {
// Open a new one after a delay
var self = this;
this.state.timeout = setTimeout(function () {
self.openWidget();
}, DELAY);
},
handleLeave: function () {
// Clear the timeout f或 opening the widget
clearTimeout(this.state.timeout);
}
...
})
both of these approaches w或k. I just want to know the reasons f或 using one over the other.