我正在使用module.exports將React.js組件轉換爲Common.js模塊,並且存在從其中一個組件的元素的上下文中訪問「this」的問題方法。module.exports中的「this」的作用域
下面是整個組件。我在問題出現的位置上方發表了評論。起初我嘗試了一個不太詳細的例子,但我認爲這不足以解釋這個問題。
var React = require('react');
var GSAP = require('gsap');
var Psychedelicon = React.createClass({
cycleColors: function() {
var touchPlatforms = ['iPhone', 'iPad', 'iPod', 'Android', 'Linux armv7l', 'WinCE'];
isTouch = false;
iDevice = false;
isDroid = false;
plat = navigator.platform;
if(plat === 'iPhone' || plat === 'iPad' || plat === 'iPod') {
isTouch = true;
iDevice = true;
}
else if (plat === 'Linux armv7l' || plat === 'Android') {
isTouch = true;
isDroid = true;
}
else {
for (var i = 0; i < touchPlatforms.length; i++) {
if (plat === touchPlatforms[i]) {
isTouch = true;
break;
}
else {
isTouch = false;
}
}
}
var isIE = false
if (navigator.userAgent.toLowerCase().indexOf('msie') > -1 || navigator.userAgent.toLowerCase().indexOf('trident') > -1) {
isIE = true
}
var isFF = false
if (navigator.userAgent.toLowerCase().indexOf('firefox') != -1) {
isFF = true
}
if(!isTouch) {
var ColorSwirl = function(colorSet,defaultColor,time) {
var storedResult;
var randomColor = function(theArray) {
var result = theArray[Math.floor(Math.random() * (theArray.length))];
if(result === storedResult){
return(defaultColor)
}
else {
storedResult = result;
return(result);
}
}
var theLuckyColors = {top:randomColor(colorSet),bottom:randomColor(colorSet)};
var swirl = function(){
//!!!!On this line the problem occurs onUpdateParams must reference the element accepting the execution event (onMouseEneter)
TweenLite.to(theLuckyColors, time, {colorProps:{top:randomColor(colorSet), bottom:randomColor(colorSet)}, onUpdate:colorize, onUpdateParams:[this],onComplete:swirl});
}
gradients
var colorize = function(el) {
if(isIE) {
TweenLite.set(el, {
backgroundImage:'-ms-radial-gradient(center,circle cover,' + theLuckyColors.top + ' 0%, ' + theLuckyColors.bottom + ' 100%)'
});
}
else if(isFF) {
TweenLite.set(el, {
backgroundImage:'-moz-radial-gradient(center,circle cover,' + theLuckyColors.top + ' 0%, ' + theLuckyColors.bottom + ' 100%)'
});
}
else {
TweenLite.set(el, {
backgroundImage:'radial-gradient(circle,' + theLuckyColors.top + ', ' + theLuckyColors.bottom + ')',
backgroundImage:'-webkit-radial-gradient(circle,' + theLuckyColors.top + ', ' + theLuckyColors.bottom + ')'
});
}
}
swirl();
}
ColorSwirl(['red','green','#4B0082','#9F00FF','yellow','orange'],'blue',.15);
}
},
stopTheCycle: function() {
},
render: function() {
return (
<a className="psychedelicon" href={this.props.href} target={this.props.target} onMouseEnter={this.cycleColors} onMouseLeave={this.stopTheCycle}>
<i className={"fa fa-" + this.props.icon}></i>
</a>
)
}
});
module.exports = Psychedelicon;
到目前爲止,我已經嘗試綁定「本」的元素接收事件:
onMouseEnter={this.cycleColors.bind(this)}
和我:''你要綁定組件的方法到組件。 React以高性能的方式自動執行此操作,因此您可以安全地刪除此調用。
我也試過:
onMouseEnter={this.cycleColors.call(Psychedelicon)}
和 OnMouseEnter在= {this.cycleColors.bind(Psychedelicon)}
這兩者產生的任何錯誤,但沒有奏效
我知道,功能否則工作,因爲當我改變時
onUpdateParams:[this]
至
onUpdateParams:['.psychedelicon']
組件產生期望的行爲,不同之處在於它的影響都在同一時間的成分(我需要避免因此需要使用「這」)的。
我必須缺少一些東西。任何幫助表示讚賞。
你的第二個代碼段也不起作用http://jsfiddle.net/arunpjohny/ssogpue2/1/ –
@ArunPJohny嗯......你說的對,我把它刪除 – HelloWorld
嘗試'$('#foo')。單擊(Module.addClass)'!在你的html代碼片段中,你甚至不會調用*方法。 – Bergi