admin管理员组文章数量:1200394
How do I call the function for getClass
for the className
inside this example? The way I have it written out does not seem to call getClass
.
var CreateList = React.createClass({
getClass: function() {
//some code to return className
},
render: function() {
return(
<div className{this.getClass}>Example</div>
);
}
});
How do I call the function for getClass
for the className
inside this example? The way I have it written out does not seem to call getClass
.
var CreateList = React.createClass({
getClass: function() {
//some code to return className
},
render: function() {
return(
<div className{this.getClass}>Example</div>
);
}
});
Share
Improve this question
asked Dec 1, 2015 at 1:55
ChipeChipe
4,80110 gold badges38 silver badges65 bronze badges
2
|
3 Answers
Reset to default 15You're referencing the instance of the getClass()
function as opposed to calling the function. Try tweaking it like so:
render: function() {
return(
<div className={this.getClass()}>Example</div>
);
}
className{this.getClass}
won't compile. Try this:
var CreateList = React.createClass({
getClass: function() {
//some code to return className
},
render: function() {
return(
<div className={this.getClass()}>Example</div>
);
}
});
If you want the div to have a class name that starts with 'className', then prepend that string to the result of the call: className={'className' + this.getClass()}
.
functional component
const statusColor = () => {
return 'red';
};
<span className={statusColor()}>your text</span>
本文标签: javascriptcall function inside of className reactjsStack Overflow
版权声明:本文标题:javascript - call function inside of className react.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738553154a2097763.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
className{this.getClass}
toclassName={'className' + this.getClass()}
. – Hunan Rostomyan Commented Dec 1, 2015 at 2:00()
. If you don't callthis.getClass
, it won't work. – Felix Kling Commented Dec 1, 2015 at 2:36