react动态添加样式

本文详细介绍了在React中如何使用ES6模板字符串和classnames库来实现动态样式的最佳实践。通过具体代码示例,展示了如何根据条件动态地添加或删除类名,以及如何利用classnames库简化这一过程,提高代码的可读性和维护性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、使用ES6模版字符串

className={`title ${index === this.state.active ? 'active' : ''}`}

二、classnames库的使用

安装:

npm install classnames --save

使用:

import classNames from 'classnames';

<div className=classNames({
    'class1': true,
    'class2': true
    )>
</div>

可以将后面的true省略,可以对传入的class进行比较明显的动态判断。

其他用法:

classNames('foo', 'bar'); // => 'foo bar'
classNames('foo', { bar: true }); // => 'foo bar'
classNames({ 'foo-bar': true }); // => 'foo-bar'
classNames({ 'foo-bar': false }); // => ''
classNames({ foo: true }, { bar: true }); // => 'foo bar'
classNames({ foo: true, bar: true }); // => 'foo bar'

// lots of arguments of various types
classNames('foo', { bar: true, duck: false }, 'baz', { quux: true }); // => 'foo bar baz quux'

// other falsy values are just ignored
classNames(null, false, 'bar', undefined, 0, 1, { baz: null }, ''); // => 'bar 1'

也可以传入数组对象:

var arr = ['b', { c: true, d: false }];
classNames('a', arr); // => 'a b c'

可以传入动态class:

let buttonType = 'primary';
classNames({ [`btn-${buttonType}`]: true });

在react中可以直接在classname内部传入动态class并进行条件判断,

var classNames = require('classnames');

var Button = React.createClass({
  // ...
  render () {
    var btnClass = classNames({
      btn: true,
      'btn-pressed': this.state.isPressed,
      'btn-over': !this.state.isPressed && this.state.isHovered
    });
    return <button className={btnClass}>{this.props.label}</button>;
  }
});

三、CSS Modules + React + classnames

import classNames from 'classnames';
import styles from './dialog.css';

export default class Dialog extends React.Component {
  render() {
    const cx = classNames({
      [styles.confirm]: !this.state.disabled,
      [styles.disabledConfirm]: this.state.disabled
    });

    return <div className={styles.root}>
      <a className={cx}>Confirm</a>
      ...
    </div>
  }
}

参考链接:

gitlab库: https://github.com/JedWatson/classnames
https://segmentfault.com/a/1190000004300065
https://blog.youkuaiyun.com/duola8789/article/details/71514450

React是一种流行的JavaScript库,用于构建用户界面。Ant Design是一种基于React的UI框架,提供了丰富的组件和工具,用于构建高质量的用户界面。 在React动态添加Ant Design的Form表单,可以使用以下步骤: 1. 创建一个React组件,用于封装Form表单。可以使用Ant Design的Form组件来创建表单,并将其添加到组件中。 ```jsx import React from 'react'; import { Form, Input } from 'antd'; const DynamicForm = () => { return ( <Form> <Form.Item> <Input /> </Form.Item> </Form> ); }; ``` 2. 使用JavaScript动态地创建Form表单元素,并将它们添加到DOM中。可以使用React的`createElement`函数来创建元素,并使用`setState`方法来更新组件的状态。当需要添加新的表单元素时,可以调用`setState`方法来更新组件的状态,并在DOM中动态地创建新的表单元素。 ```jsx class FormContainer extends React.Component { state = { dynamicFields: ['name', 'age'], // 初始字段列表 }; addField = () => { const { dynamicFields } = this.state; dynamicFields.push('email'); // 添加新的字段到列表中 this.setState({ dynamicFields }); // 更新状态并触发重新渲染组件 }; render() { return ( <div> <button onClick={this.addField}>添加字段</button> <DynamicForm /> // 使用动态生成的表单元素渲染动态表单 </div> ); } } ``` 在上面的代码中,我们创建了一个名为`FormContainer`的React组件,它包含一个状态变量`dynamicFields`,用于存储动态生成的表单字段列表。当用户点击“添加字段”按钮时,将触发`addField`方法,将一个新的字段添加到列表中,并使用`setState`方法更新状态。这将触发组件的重新渲染,并使用动态生成的表单元素渲染动态表单。 请注意,动态添加表单元素需要使用适当的布局和样式来正确显示它们。Ant Design的Form组件提供了一些有用的布局和样式选项,可以帮助您更好地控制表单的外观和行为。您可以参考Ant Design的文档来了解更多关于Form组件的信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值