创建一个步进器组件,包含当前步骤(currentStep)的状态以及前进和后退的操作:
import React, { useState } from 'react'; function Stepper() { const [currentStep, setCurrentStep] = useState(1); const handleNext = () => { setCurrentStep(currentStep + 1); }; const handlePrevious = () => { setCurrentStep(currentStep - 1); }; return ( <div> <h2>Current Step: {currentStep}</h2> <button onClick={handlePrevious} disabled={currentStep === 1}> Previous </button> <button onClick={handleNext} disabled={currentStep === 3}> Next </button> </div> ); } export default Stepper;
在应用中使用该步进器组件:
import React from 'react'; import Stepper from './Stepper'; function App() { return ( <div> <Stepper /> {/* 其他组件 */} {/* ... */} </div> ); } export default App;