I thought it was the case that the this
keyword always refers to the component in React, but in my toggleStyle
method in the below this
is returning the window object instead of the React component object . Can somebody help me to understand why that is?
renderDeviceToggle() {
var arrowAnimation = {
rotateX: this.state.expanded ? 180 : 0,
transformOriginY: [ '42%', '42%' ]
};
let toggleState = function () {
// 'this' is the window
console.log("in toggleState: this: ", this)
this.setState({expanded: !this.state.expanded});
};
return (
<div className="device-toggle" onClick={toggleState}>
<div>{console.log("in return: this is the component: ", this)}</div>
<div className="device-icon icon huge"></div>
<VelocityComponent duration={300} animation={arrowAnimation}>
<div className="icon color-blue flex">
<i className="icon-caret-down"></i>Hi
</div>
</VelocityComponent>
</div>
);
},
render() {
return (
<div>
<div className="panel panel-default">
<div className="panel-heading">
<div className="pull-right text-right">
<p><a href="/addOrder" className="pull-right">New order </a></p>
<p><a href="#"> View all</a></p>
</div>
<h4>Top orders</h4>
</div>
<div className="panel-body">
{ this.data.orders ?
this.renderOrderTable() :
<p>Loading</p> }
</div>
<div>
{this.renderDeviceToggle()}
</div>
</div>
</div>
);
}