【发布时间】:2015-09-11 12:17:35
【问题描述】:
我正在使用 module.exports 将 React.js 组件转换为 Common.js 模块,并且在从其中一种方法访问组件元素上下文中的“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;
到目前为止,我已经尝试将“this”绑定到接收事件的元素:
onMouseEnter={this.cycleColors.bind(this)}
我得到:`'你正在将一个组件方法绑定到组件。 React 会以高性能的方式自动为您执行此操作,因此您可以安全地删除此调用。'
我也试过了:
onMouseEnter={this.cycleColors.call(Psychedelicon)}
和 onMouseEnter={this.cycleColors.bind(Psychedelicon)}
两者都没有产生错误但没有工作
我知道该功能可以正常工作,因为当我更改时
onUpdateParams:[this]
到
onUpdateParams:['.psychedelicon']
组件产生所需的行为,除了它同时影响所有组件(我需要避免因此需要使用“this”)。
我一定错过了什么。任何帮助表示赞赏。
【问题讨论】:
-
你的第二个 sn-p 也不起作用jsfiddle.net/arunpjohny/ssogpue2/1
-
@ArunPJohny 嗯...你是对的,我删除了它
-
试试
$('#foo').click(Module.addClass)!在您的 html sn-p 中,您甚至没有调用该方法。
标签: javascript reactjs commonjs node-modules