8、React中classSet的用法

前端之家收集整理的这篇文章主要介绍了8、React中classSet的用法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本篇记录如何使用React中的classSet,为一个DOM节点指定多个不同的class。

如果我们要为一个DOM节点设置多个不同的class,一般是下面这种写法:

  1. <input type="text" class="class1 class2 class3" />
在React中,如果不使用classSet,要为一个DOM节点指定多个不同的class,可能是下面这种写法:
  1. var App = React.createClass({
  2. render: function() {
  3. var classString = "";
  4. if(this.props.important) {
  5. classString = "important";
  6. }else{
  7. classString = "normal";
  8. }
  9. return (
  10. <p className={classString}>{this.props.text}</p>
  11. );
  12. }
  13. });
在上面的代码中,通过important属性,来判断是否要给<p>标签添加名为important的class,其中important和normal的css样式如下:
  1. <style type="text/css">
  2. .important {
  3. color: red;
  4. }
  5. .normal {
  6. color: black;
  7. }
  8. </style>
上面这种写法虽然可以达到效果,但是当class多起来的时候,可能用起来就不是很方便了,所以React为我们提供了一种简单的操纵class属性的方式,即classSet。

关于classSet的用法,用下面的代码来说明:

  1. var App2 = React.createClass({
  2. render: function() {
  3. var cx = React.addons.classSet;
  4. var classes = cx({
  5. "important": this.props.important == true,"normal": this.props.important == false
  6. });
  7. return (
  8. <p className={classes}>{this.props.text}</p>
  9. );
  10. }
  11. });
classSet是React.addons中的一个对象,需要引入react-with-addons.js文件才可以使用,上面的代码中,classes返回的即包含了多个class属性的字符串,是否需要包含important或者normal,需要根据后面的表达式来确定,若表达式的值为true,则表示需要对应的class,若为false,则代表不需要对应的class,下面用一个demo记录classSet的用法,在浏览器中运行的结果如下图:


下面是代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <Meta charset="UTF-8">
  5. <title>classSet用法</title>
  6. <script type="text/javascript" src="../react-0.13.0/build/react-with-addons.js"></script>
  7. <script type="text/javascript" src="../react-0.13.0/build/JSXTransformer.js"></script>
  8. <style type="text/css">
  9. .important {
  10. color: red;
  11. }
  12. .normal {
  13. color: black;
  14. }
  15. .bigText {
  16. font-size: 40px;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <script type="text/jsx">
  22. var App = React.createClass({
  23. render: function() {
  24. var classString = "";
  25. if(this.props.important) {
  26. classString = "important";
  27. }else{
  28. classString = "normal";
  29. }
  30. return (
  31. <p className={classString}>{this.props.text}</p>
  32. );
  33. }
  34. });
  35. var App2 = React.createClass({
  36. render: function() {
  37. var cx = React.addons.classSet;
  38. var classes = cx({
  39. "important": this.props.important == true,"normal": this.props.important == false
  40. });
  41. return (
  42. <p className={classes}>{this.props.text}</p>
  43. );
  44. }
  45. });
  46. var App3 = React.createClass({
  47. render: function() {
  48. var cx = React.addons.classSet;
  49. var classes = cx("bigText","important");
  50. return (
  51. <p className={classes}>{this.props.text}</p>
  52. );
  53. }
  54. });
  55. React.render(
  56. <div>
  57. 1. 为不同属性设置不同class的普通做法,用js代码判断是否需要添加class<br/>
  58. <App important={false} text="this is not important string." />
  59. <App important={true} text="this is important string." />
  60. <hr/>
  61. 2. 为不同属性设置不同class的使用classSet的做法,classSet会根据表达式的值,自动设置class<br/>
  62. <App2 important={false} text="this is not important string." />
  63. <App2 important={true} text="this is important string." />
  64. <hr/>
  65. 3. classSet使用多个class,将文本变大变红<br/>
  66. <App3 text="Hello world!" />
  67. </div>,document.body
  68. );
  69. </script>
  70. </body>
  71. </html>

猜你在找的React相关文章