ReactJS将动态类添加到手动类名 [英] ReactJS add dynamic class to manual class names

查看:509
本文介绍了ReactJS将动态类添加到手动类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要添加一个动态类到常规类的列表,但不知道如何(我使用babel),像这样:

I need to add a dynamic class to a list of regular classes, but have no idea how (I'm using babel), something like this:

<div className="wrapper searchDiv {this.state.something}">
...
</div>

任何想法?

p>

推荐答案

您可以这样做,正常的javascript:

You can either do this, normal javascript:

className = {'wrapper searchDiv'+ this.state.something}

或字符串模板版本

className = {`wrapper searchDiv $ {this.state.something}`} 有反引号。

两种类型当然都是javascript,但第一种模式是传统类型。无论如何,在JSX中,用大括号括起来的任何东西都是作为javascript执行的,所以你基本上可以做任何你想要的。但是组合JSX字符串大括号是属性的一个禁用。

Both types are of course just javascript, but the first pattern is the traditional kind. Anyway, in JSX, anything enclosed in curly brackets is executed as javascript, so you can basically do whatever you want there. But combining JSX strings and curly brackets is a no-go for attributes.

这篇关于ReactJS将动态类添加到手动类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆