为什么我们需要在渲染功能中使用var self = this;
?我认为,渲染中的this
是指我的ServiceChooser
组件。我错了吗?有人可以解释一下吗?
var ServiceChooser = React.createClass({
getInitialState: function(){
return { total: 0 };
},
addTotal: function( price ){
this.setState( { total: this.state.total + price } );
},
render: function() {
**var self = this;**
var services = this.props.items.map(function(s){
return <Service name={s.name} price={s.price} active={s.active} addTotal={self.addTotal} />;
});
return <div>
<h1>Our services</h1>
<div id="services">
{services}
<p id="total">Total <b>${this.state.total.toFixed(2)}</b></p>
</div>
</div>;
}
});