No react.js, é melhor armazenar uma referência de tempo limite como uma variável de instância (this.timeout) ou uma variável de estado (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 for opening the widget
clearTimeout(this.timeout);
}
...
})
ou
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 for opening the widget
clearTimeout(this.state.timeout);
}
...
})
ambas as abordagens funcionam. Eu só quero saber as razões para usar um sobre o outro.
this.timeout = setTimeout(this.openWidget, DELAY);
this.statediretamente, pois a chamadasetState()posterior poderá substituir a mutação que você fez. Tratethis.statecomo se fosse imutável."