🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)
🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)

🔧 Programmierung 🕛 kürzlich 4 Min Lesezeit
0

A Simple Tutorial on the useState Hook in React

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

This is the simplest tutorial to get you started with the useState hook.



We'll use a calculator app as an example.






Here's the final code:






CODE
import "./styles.css";
import {useState} from 'react'

export default function App() {
const [counter, setCounter] = useState(0)

const handleIncrement = () => {
setCounter(counter +1)
}
const handleDecrement = () => {
if(counter > 0) {
setCounter(counter -1)
}
}

return (
<div className="App">
<h1>Counter App to understand the useState Hook</h1>
<h2>{counter}</h2>
<button onClick={handleIncrement}>increment</button>
<button onClick={handleDecrement}>Decrement</button>
</div>
);
}









What is the useState hook ?



The official definition from the React documentation is: "a React Hook that lets you add a state variable to your component." In simpler terms, it's a variable that will make your component re-render (reload) when its value changes.






How to use it?



First let's start with a simple component with a title, a counter, and increment and decrement buttons.




CODE
import "./styles.css";

export default function App() {
return (
<div className="App">
<h1>Counter App to understand the useState Hook</h1>
<h2>0</h2>
<button>increment</button>
<button>Decrement</button>
</div>
);
}






We then import the useState hook and call it at the top of the component to declare a state variable:




CODE
import "./styles.css";
import {useState} from 'react'

export default function App() {
const [counter, setCounter] = useState(0)

return (
// ...
}






Here we called our state variables, [counter, setCounter], where counter will hold the state value, and setCounter will be the function to update this value. We also assign an initial state value, useState(0), but it can accept other types, like strings, booleans, or more.



Now we'll make a function called handleIncrement and assign it to the "onClick" event of the Increment button that will trigger the function each time the button is clicked. We will also remove the hard coded value inside the h2 tag and replace it with {counter}.




CODE
// ...

export default function App() {
const [counter, setCounter] = useState(0)

const handleIncrement = () => {
setCounter(counter +1)
}

return (
<div className="App">
<h1>Counter App to understand the useState Hook</h1>
<h2>{counter}</h2>
<button onClick={handleIncrement}>increment</button>
<button>Decrement</button>
</div>
);
}






As you can see we didn't increment the counter by writing counter = counter + 1 like a regular variable in Javascript but we used setCounter(counter + 1) which is the only way you can change the value of a state variable.



Let's also add a decrement function.




CODE
import "./styles.css";
import {useState} from 'react'

export default function App() {
const [counter, setCounter] = useState(0)

const handleIncrement = () => {
setCounter(counter +1)
}

const handleDecrement = () => {
setCounter(counter -1)
}

return (
<div className="App">
<h1>Counter App to understand the useState Hook</h1>
<h2>{counter}</h2>
<button onClick={handleIncrement}>increment</button>
<button onClick={handleDecrement}>Decrement</button>
</div>
);
}






Finally, we'll add an if statement in our handleDecrement function to prevent the counter from being a negative value.




CODE
import "./styles.css";
import {useState} from 'react'

export default function App() {
const [counter, setCounter] = useState(0)

const handleIncrement = () => {
setCounter(counter +1)
}
const handleDecrement = () => {
if(counter > 0) {
setCounter(counter -1)
}
}

return (
<div className="App">
<h1>Counter App to understand the useState Hook</h1>
<h2>{counter}</h2>
<button onClick={handleIncrement}>increment</button>
<button onClick={handleDecrement}>Decrement</button>
</div>
);
}






If there are no errors than congrats! you have officially learned the most important and most used React hook.



This wraps up our tutorial! but don't get too excited because now you'll need to practice until you master it.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
GPT-6 Astra Release Today? OpenAI’s Next Major AI Model Is Almost Here
1 Quelle
Apple accuses OpenAI of destroying evidence as trade-secrets fight intensifies
1 Quelle
Major AI platforms go down in unprecedented simultaneous outage
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten A Simple Tutorial on the useState Hook in React

Thematisch verwandte Begriffe: Simple, Tutorial, useState, Hook · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...