reactjs - Defining attributes in React.js -
i wondering there difference between these 2 ways of defining react component attributes:
var = react.createclass({ ssestream: new eventsource("/stream/"), componentdidmount: function() { this.ssestream.addeventlistener("message", function(msg) { // }.bind(this)); }); var = react.createclass({ componentdidmount: function() { this.ssestream = new eventsource("/stream/"); this.ssestream.addeventlistener("message", function(msg) { // }.bind(this)); } });
note difference of how react component attribute ssestream defined. guess in second example attribute being recreated every time component re-rendered whereas in first created once , therefore first way should preferred.
so question is, there slightest difference between two?
the difference between 2 follows:
the first instantiates , sets single eventsource
@ time component declared, shared between each instance of component.
on other hand, second creates separate eventsource
each instance of component, when callback fired.
assuming want multiple instances of component independent of 1 another, guess second option want.
by way, componentdidmount
callback typically run once in life-cycle of component, when component first mounted, has nothing re-renders.
Comments
Post a Comment