16

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>
        );
    }
});
2
  • 1
    Try changing className{this.getClass} to className={'className' + this.getClass()}. Commented Dec 1, 2015 at 2:00
  • Functions are called with (). If you don't call this.getClass, it won't work. Commented Dec 1, 2015 at 2:36

3 Answers 3

15

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>
    );
}
Sign up to request clarification or add additional context in comments.

2 Comments

Is it possible to send div element to getClass() as parameter?
@AmirHosseinRezaei I'm not 100% sure I understand your question, but you could in theory rewrite getClass to accept a node and work on it in getClass. You would have to first assign the div to a variable, update getClass to accept a param, and then call it. I'm really unsure about why you would do that to be honest. Why would you need the div to decide what class it should have?
2

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

Comments

0

functional component

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.