State
Was ist State?
State-Variable erstellen
const [step, setStep] = useState(1);const App = () => {
const [step, setStep] = useState(1);
const handleNext = () => {
if (step < 3) setStep((step) => step + 1);
};
const handlePrevious = () => {
if (step > 1) setStep((step) => step + 1);
};
return (
<div className="steps">
<div className="numbers">
<div className={`${step >= 1 ? 'active' : ''}`}>1</div>
<div className={`${step >= 2 ? 'active' : ''}`}>2</div>
<div className={`${step >= 3 ? 'active' : ''}`}>3</div>
</div>
<p className="message">
Step {step}: {messages[step - 1]}
</p>
<div className="buttons">
<button
onClick={handlePrevious}
style={{ backgroundColor: '#7950f2', color: '#ffffff' }}
>
Previous
</button>
<button
onClick={handleNext}
style={{ backgroundColor: '#7950f2', color: '#ffffff' }}
>
Next
</button>
</div>
</div>
);
};
export default App;Hooks
Aufbau von useState
useStateLast updated