In react.js, è meglio memorizzare un riferimento di timeout come variabile di istanza (this.timeout) o una variabile di stato (this.state.timeout)?Istanza v variabili di stato in react.js
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);
}
...
})
o
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);
}
...
})
entrambi questi approcci funzionano. Voglio solo sapere i motivi per usare l'uno sull'altro.
Dal [documentazione] (http://facebook.github.io/react/docs/component-api.html): * "** ** MAI mutano' this.state' direttamente, come chiamare 'setState()' può in seguito sostituire la mutazione che hai fatto. Tratta 'this.state' come se fosse immutabile." * –
Suggerimento: Usa l'autobinding di React: 'this.timeout = setTimeout (this.openWidget, DELAY);' – David
A cosa dovrebbe essere impostato DELAY? – justingordon