当前位置:首页 > React

react实现展开收起功能

2026-01-27 16:39:43React

实现展开收起功能的方法

在React中实现展开收起功能可以通过状态管理和条件渲染来完成。以下是几种常见的实现方式:

使用useState钩子管理状态

通过React的useState钩子来管理展开和收起的状态,结合条件渲染实现功能。

import React, { useState } from 'react';

function ExpandCollapse() {
  const [isExpanded, setIsExpanded] = useState(false);

  const toggleExpand = () => {
    setIsExpanded(!isExpanded);
  };

  return (
    <div>
      <button onClick={toggleExpand}>
        {isExpanded ? '收起' : '展开'}
      </button>
      {isExpanded && <div>这里是展开的内容</div>}
    </div>
  );
}

export default ExpandCollapse;

使用CSS过渡动画

如果需要平滑的过渡效果,可以结合CSS实现动画。

react实现展开收起功能

import React, { useState } from 'react';
import './ExpandCollapse.css';

function ExpandCollapse() {
  const [isExpanded, setIsExpanded] = useState(false);

  const toggleExpand = () => {
    setIsExpanded(!isExpanded);
  };

  return (
    <div>
      <button onClick={toggleExpand}>
        {isExpanded ? '收起' : '展开'}
      </button>
      <div className={`content ${isExpanded ? 'expanded' : 'collapsed'}`}>
        这里是展开的内容
      </div>
    </div>
  );
}

export default ExpandCollapse;

对应的CSS文件:

.content {
  overflow: hidden;
  transition: max-height 0.3s ease;
}

.collapsed {
  max-height: 0;
}

.expanded {
  max-height: 500px; /* 根据内容调整 */
}

使用第三方库

如果需要更复杂的功能,可以考虑使用第三方库如react-collapse

react实现展开收起功能

import React, { useState } from 'react';
import { Collapse } from 'react-collapse';

function ExpandCollapse() {
  const [isExpanded, setIsExpanded] = useState(false);

  const toggleExpand = () => {
    setIsExpanded(!isExpanded);
  };

  return (
    <div>
      <button onClick={toggleExpand}>
        {isExpanded ? '收起' : '展开'}
      </button>
      <Collapse isOpened={isExpanded}>
        <div>这里是展开的内容</div>
      </Collapse>
    </div>
  );
}

export default ExpandCollapse;

动态内容高度

如果内容高度不确定,可以通过动态计算高度实现更精确的展开收起效果。

import React, { useState, useRef, useEffect } from 'react';

function ExpandCollapse() {
  const [isExpanded, setIsExpanded] = useState(false);
  const [contentHeight, setContentHeight] = useState(0);
  const contentRef = useRef(null);

  useEffect(() => {
    if (contentRef.current) {
      setContentHeight(contentRef.current.scrollHeight);
    }
  }, []);

  const toggleExpand = () => {
    setIsExpanded(!isExpanded);
  };

  return (
    <div>
      <button onClick={toggleExpand}>
        {isExpanded ? '收起' : '展开'}
      </button>
      <div
        ref={contentRef}
        style={{
          overflow: 'hidden',
          transition: 'max-height 0.3s ease',
          maxHeight: isExpanded ? `${contentHeight}px` : '0',
        }}
      >
        这里是展开的内容
      </div>
    </div>
  );
}

export default ExpandCollapse;

多级展开收起

对于多级展开收起功能,可以通过嵌套组件或递归实现。

import React, { useState } from 'react';

function ExpandCollapseItem({ title, children }) {
  const [isExpanded, setIsExpanded] = useState(false);

  const toggleExpand = () => {
    setIsExpanded(!isExpanded);
  };

  return (
    <div>
      <button onClick={toggleExpand}>
        {isExpanded ? '收起' : '展开'} {title}
      </button>
      {isExpanded && <div>{children}</div>}
    </div>
  );
}

function MultiLevelExpandCollapse() {
  return (
    <div>
      <ExpandCollapseItem title="一级标题">
        <ExpandCollapseItem title="二级标题">
          <div>这里是二级内容</div>
        </ExpandCollapseItem>
      </ExpandCollapseItem>
    </div>
  );
}

export default MultiLevelExpandCollapse;

标签: 功能react
分享给朋友:

相关文章

vue实现审核功能

vue实现审核功能

实现审核功能的基本思路 审核功能通常涉及状态管理、权限控制和操作记录。Vue中可以通过组件化设计、状态管理库(如Vuex或Pinia)和后端API配合实现。 审核状态管理 使用Vuex或Pinia存…

如何优化react

如何优化react

优化 React 性能的方法 使用 React.memo 或 PureComponent 对于函数组件,使用 React.memo 进行记忆化,避免不必要的重新渲染。类组件可以使用 PureCompo…

如何改造react

如何改造react

改造 React 项目的关键方法 分析当前项目结构 通过评估现有组件、状态管理和依赖项,明确需要改进的部分。使用工具如 webpack-bundle-analyzer 识别性能瓶颈。 升级 Reac…

react如何查

react如何查

React 查询方法 React 提供了多种查询 DOM 元素的方式,以下是几种常见的方法: 使用 ref 通过 useRef 钩子可以获取 DOM 节点的引用,适用于直接操作 DOM 的场景。…

如何开发react

如何开发react

开发React应用的基本步骤 安装Node.js和npm 确保系统中已安装Node.js(包含npm)。可通过官网下载安装包,安装后验证版本: node -v npm -v 创建React项目…

如何恢复react

如何恢复react

恢复 React 项目的方法 检查并修复依赖项 运行 npm install 或 yarn install 重新安装所有依赖项。如果依赖项损坏或缺失,这将恢复项目所需的库和工具。 恢复删除的文件 如…