【React】自定义组件-文本展开收起组件

本文介绍如何使用React和Ant Design库创建一个可扩展的文本组件,用户可以点击按钮展开或收起长文本,适合内容管理。通过useState管理组件状态,实现实体隐藏与显示功能。

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

参考文章:

长文本展开收起功能实现

1、文本展开收起组件

import React, { PureComponent } from 'react';
import { Typography } from 'antd';
const { Paragraph } = Typography;

/** 文本隐藏/展开组件。参数(rows,content) */
class ExpandableText extends PureComponent {
  state = {
    expand: false,
    counter: 0
  };

  typoExpand = () => {
    this.setState({
      expand: true,
      counter: !this.state.expand
        ? this.state.counter + 0
        : this.state.counter + 1
    });
  };

  typoClose = () => {
    this.setState({
      expand: false,
      counter: !this.state.expand
        ? this.state.counter + 0
        : this.state.counter + 1
    });
  };

  render() {
    const { rows = 3, content } = this.props;
    const { expand, counter } = this.state;
    return (
      <div>
        <div key={counter}>
          <Paragraph ellipsis={{ rows: 3, expandable: true, onExpand: this.typoExpand }} >
            {content}
          </Paragraph>
        </div>
        {expand && <a onClick={this.typoClose}>Close</a>}
      </div>
    );
  }
}

export default ExpandableText;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值