call function inside of className react.js

前端 未结 2 1355
小蘑菇
小蘑菇 2021-01-11 10:28

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

相关标签:
2条回答
  • 2021-01-11 10:50

    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()}.

    0 讨论(0)
  • 2021-01-11 10:55

    You'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>
        );
    }
    
    0 讨论(0)
提交回复
热议问题