簡體   English   中英

在 className react.js 中調用 function

[英]call function inside of className react.js

如何在這個示例中為classNamegetClass調用 function? 我寫出來的方式似乎沒有調用getClass

var CreateList = React.createClass({
    getClass: function() {
        //some code to return className
    },
    render: function() {
        return(
            <div className{this.getClass}>Example</div>
        );
    }
});

您正在引用getClass()函數的實例,而不是調用該函數。 嘗試調整它如下:

render: function() {
    return(
        <div className={this.getClass()}>Example</div>
    );
}

className{this.getClass}將無法編譯。 嘗試這個:

var CreateList = React.createClass({
    getClass: function() {
        //some code to return className
    },
    render: function() {
        return(
            <div className={this.getClass()}>Example</div>
        );
    }
});

如果您希望div具有以“className”開頭的類名,則將該字符串添加到調用的結果中: className={'className' + this.getClass()}

功能組件

const statusColor = () => {
    return 'red';
  };
<span className={statusColor()}>your text</span>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM