当前位置:首页 > React

react实现steps步骤条

2026-01-27 17:13:56React

React 实现 Steps 步骤条

在 React 中实现 Steps 步骤条可以通过多种方式完成,以下是一种常见的实现方法:

使用 Ant Design 的 Steps 组件

Ant Design 提供了一个现成的 Steps 组件,可以快速实现步骤条功能。

安装 Ant Design:

npm install antd

引入 Steps 组件并使用:

import { Steps } from 'antd';
const { Step } = Steps;

function App() {
  return (
    <Steps current={1}>
      <Step title="第一步" description="填写基本信息" />
      <Step title="第二步" description="验证身份" />
      <Step title="第三步" description="完成注册" />
    </Steps>
  );
}

export default App;

自定义 Steps 组件

如果需要完全自定义 Steps 组件,可以手动实现:

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

function CustomSteps({ steps, currentStep }) {
  return (
    <div className="steps-container">
      {steps.map((step, index) => (
        <div
          key={index}
          className={`step ${index <= currentStep ? 'active' : ''}`}
        >
          <div className="step-number">{index + 1}</div>
          <div className="step-title">{step.title}</div>
          <div className="step-description">{step.description}</div>
        </div>
      ))}
    </div>
  );
}

function App() {
  const [currentStep, setCurrentStep] = useState(0);
  const steps = [
    { title: '第一步', description: '填写基本信息' },
    { title: '第二步', description: '验证身份' },
    { title: '第三步', description: '完成注册' },
  ];

  return (
    <div>
      <CustomSteps steps={steps} currentStep={currentStep} />
      <button onClick={() => setCurrentStep(currentStep + 1)}>下一步</button>
    </div>
  );
}

export default App;

对应的 CSS 文件:

.steps-container {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.step {
  flex: 1;
  text-align: center;
  position: relative;
}

.step-number {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background-color: #ccc;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 10px;
}

.step.active .step-number {
  background-color: #1890ff;
  color: white;
}

.step-title {
  font-weight: bold;
}

.step-description {
  color: #666;
  font-size: 14px;
}

使用 Material-UI 的 Stepper 组件

Material-UI 也提供了 Stepper 组件:

安装 Material-UI:

npm install @mui/material @emotion/react @emotion/styled

引入并使用 Stepper:

react实现steps步骤条

import React from 'react';
import Stepper from '@mui/material/Stepper';
import Step from '@mui/material/Step';
import StepLabel from '@mui/material/StepLabel';

function App() {
  const steps = ['第一步', '第二步', '第三步'];
  const [activeStep, setActiveStep] = React.useState(0);

  return (
    <div>
      <Stepper activeStep={activeStep}>
        {steps.map((label) => (
          <Step key={label}>
            <StepLabel>{label}</StepLabel>
          </Step>
        ))}
      </Stepper>
      <button onClick={() => setActiveStep(activeStep + 1)}>下一步</button>
    </div>
  );
}

export default App;

以上方法提供了从简单到复杂的多种实现方式,可以根据项目需求选择合适的方案。

标签: 步骤react
分享给朋友:

相关文章

react moment如何使用

react moment如何使用

安装 react-moment 通过 npm 或 yarn 安装 react-moment 包。确保项目中已安装 moment.js,因为 react-moment 依赖它。 npm install…

react如何衰减

react如何衰减

React 中的动画衰减效果实现 在 React 中实现衰减效果(如滚动衰减、拖动释放后的惯性滑动)通常需要结合物理动画原理或第三方动画库。以下是几种常见方法: 使用 CSS 动画和 @keyfra…

如何同步react

如何同步react

同步React组件状态的方法 使用useState和useEffect钩子组合可以同步React组件的状态。useState用于声明状态变量,useEffect用于监听状态变化并执行副作用。 imp…

如何运行react

如何运行react

运行 React 项目的步骤 安装 Node.js 和 npm 确保系统已安装 Node.js(包含 npm)。可通过官网 Node.js 下载并安装最新稳定版本。安装完成后,验证版本: node…

react如何重启

react如何重启

重启 React 应用的方法 重新加载当前页面 使用 window.location.reload() 强制刷新页面,这会重新加载整个应用并重置所有状态。 window.location…

react 如何修改state

react 如何修改state

修改 state 的基础方法 在 React 中,state 的修改必须通过 setState 方法(类组件)或 state 更新函数(函数组件)。直接修改 state 会导致组件不会重新渲染,且可能…