Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a dynamic class name

In react.js I need to add a dynamic class name to a div.
Using react-addons, I tried it the following way but in vain:

var addons = require('react-addons');
var cx = addons.classSet;   

var Overlay = React.createClass({

    render: function() {
        var prod_id = this.props.prop_id;
        var large_prod_class = 'large_prod_modal_' + prod_id;

        var modal_classes = cx({
            'large_prod_modal': true,
            large_prod_class: true,
            'hidden': true
        });

        return (<div className={modal_classes}>lorem ipsum</div>);
    }
});

And Overlay component is used in the following way:

<Overlay prod_id="9" />

The prop (i.e: prod_id) value is random however. I do not get the large_prod_modal_9 class for the div. All the classes I get are large_prod_modal,large_prod_class and hidden

How to get the large_prod_modal_9 class for the div?

like image 826
Istiaque Ahmed Avatar asked Feb 13 '15 10:02

Istiaque Ahmed


People also ask

How do I add a class dynamically?

To do that, first we create a class and assign it to HTML elements on which we want to apply CSS property. We can use className and classList property in JavaScript. Approach: The className property used to add a class in JavaScript.

How do I add a class to a dynamic element in React?

Use a template literal to dynamically add CSS classes to React elements. Template literals are delimited with backticks and allow us to embed variables and expressions using the dollar sign and curly braces ${expression} syntax.

How can you add a class name in JS?

Using . add() method: This method is used to add a class name to the selected element.


2 Answers

The classSet addon is deprecated as of 0.13. The offical recomendation is to use JedWatson/classnames.

var cx = require('classnames');   
var Overlay = React.createClass({

    render: function() {
        var prod_id = this.props.prop_id;
        var modal_classes  = cx('large_prod_modal_' + prod_id, {
            'large_prod_modal': true,
            'hidden': true
        });

        return (<div className={modal_classes}>lorem ipsum</div>);

    }
});

If all of the class names are always true, you can just pass them as strings.

var prod_id = this.props.prop_id;
var modal_classes  = cx(
    'large_prod_modal_' + prod_id,
    'large_prod_modal'
    'hidden'
});

You can mix strings and objects as desired. You don't get this flexibility with the addon classSet.

like image 189
Brigand Avatar answered Sep 17 '22 14:09

Brigand


example solution for this would be:

 dynamicClass: function(){
     return "large_prod_modal large_prod_modal_" + this.props.prod_id + " hidden"
 }
 render: function(){
   return (<div className={this.dynamicClass()}>lorem ipsum</div>) 
 }

You can't generate dynamicly the key in JSON object so thats why you get 'large_prod_class' and it's correct

like image 35
Piotr Karbownik Avatar answered Sep 21 '22 14:09

Piotr Karbownik