简体   繁体   English

添加动态类名称

[英]Add a dynamic class name

In react.js I need to add a dynamic class name to a div . react.js中,我需要为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组件以下列方式使用:

<Overlay prod_id="9" />

The prop (ie: prod_id ) value is random however. 然而,prop(即: prod_id )值是随机的。 I do not get the large_prod_modal_9 class for the div . 我没有得到divlarge_prod_modal_9类。 All the classes I get are large_prod_modal , large_prod_class and hidden 我得到的所有类都是large_prod_modallarge_prod_classhidden

How to get the large_prod_modal_9 class for the div ? 如何获取divlarge_prod_modal_9类?

The classSet addon is deprecated as of 0.13. classSet插件从0.13开始不推荐使用。 The offical recomendation is to use JedWatson/classnames . 官方推荐是使用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. 如果所有类名都是true,则可以将它们作为字符串传递。

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. 你没有使用addon classSet获得这种灵活性。

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 你不能动态生成JSON对象中的键,这就是为什么你得到'large_prod_class'并且它是正确的

You can use this: 你可以用这个:

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

You can see this documentation about adding dynamic properties to objects at run time (dynamically): 您可以在运行时(动态)查看有关向对象添加动态属性的文档

This notation is also very useful when property names are to be dynamically determined (when the property name is not determined until runtime). 当要动态确定属性名称时(直到运行时才确定属性名称),此表示法也非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM