🪟 Windows TippsHow to Enable Windows 11 Screen Savers(07.09.2026 um 12:41 Uhr)
🪟 Windows TippsMicrosoft Phone Link Not Showing Messages on Windows 11? Fix It(09.09.2026 um 07:52 Uhr)
⚠️ Malware / Trojaner / VirenPost-DEF CON phishing campaign delivered AMOS and NetSupport malware(24.08.2026 um 09:42 Uhr)
💾 IT Security ToolsHow to Use BloodHound Active Directory Setup Attack Path Analysis(10.09.2026 um 14:35 Uhr)
🕵️ SicherheitslückenCompliance Alert: EU Cyber Resilience Act 24-Hour Reporting Enforced(11.09.2026 um 06:25 Uhr)
🕵️ SicherheitslückenAWS IAM Privilege Escalation: Cheat Sheet And Defense(11.09.2026 um 07:43 Uhr)
🕵️ SicherheitslückenArista warns customers ahead of next week’s security disclosures(02.09.2026 um 23:34 Uhr)
🕵️ SicherheitslückenKARR Security vulnerability(02.09.2026 um 03:15 Uhr)
🪟 Windows TippsHow to Enable Windows 11 Screen Savers(07.09.2026 um 12:41 Uhr)
🪟 Windows TippsMicrosoft Phone Link Not Showing Messages on Windows 11? Fix It(09.09.2026 um 07:52 Uhr)
⚠️ Malware / Trojaner / VirenPost-DEF CON phishing campaign delivered AMOS and NetSupport malware(24.08.2026 um 09:42 Uhr)
💾 IT Security ToolsHow to Use BloodHound Active Directory Setup Attack Path Analysis(10.09.2026 um 14:35 Uhr)
🕵️ SicherheitslückenCompliance Alert: EU Cyber Resilience Act 24-Hour Reporting Enforced(11.09.2026 um 06:25 Uhr)
🕵️ SicherheitslückenAWS IAM Privilege Escalation: Cheat Sheet And Defense(11.09.2026 um 07:43 Uhr)
🕵️ SicherheitslückenArista warns customers ahead of next week’s security disclosures(02.09.2026 um 23:34 Uhr)
🕵️ SicherheitslückenKARR Security vulnerability(02.09.2026 um 03:15 Uhr)

🔧 Programmierung 🕛 vor 2 Jahren 4 Min Lesezeit
0

Hooks in React

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




1 Definitions :



To explain the concept of hooks in React, it is important to start by explaining the terms below :





  • ✔️A React component is a function that renders an HTML element. React components can be either classes or functions.


  • ✔️React classes class components) use the React lifecycle to manage state and component events.


  • ✔️The React lifecycle is a set of events that occur during the life of a React component. Lifecycle events allow developers to interact with the React component at different points in its life.


  • ✔️React functions (functions components) use hooks to manage state and component events.








2. What is a Hook?






  • Hooks are functions that allow developers to manage state and access React lifecycle features without having to write a class.




To explain hooks, we can use the following analogy:





  • React classes are like washing machines. They have a life cycle that allows them to wash the laundry.


  • React functions are like washing machines without a lifecycle. They must be used with specific tools for washing laundry.





Hooks are these specific tools ⚙️ They allow React functions to access React lifecycle features.



- 🔸 Le Hook UseState() :





  • The useState() Hook : Allows you to manage the state of a React component.




CODE
function App() {
const [number, setNumber] = useState(0);

return (
<div>
<h1>Number: [number)</h1>
<button onClick={() => setNumber(number+ 1))>+1</button>
</div>
);
)






This React component uses the useState() hook to manage the state of the number . The number variable represents the current state of the number. The setNumber() function allows you to modify the state of the number .



➡️The useState() hook returns 2 values : the current value of the state & a function that allows to modify the state.



📌There are many hooks available, each with their own purpose.



🗃️Here are some of the most common hooks:




useState(): Allows you to manage the state of a React component.

useEffect(): Allows you to react to changes in the environment of a React component.

useContext(): Allows access to a context from a React component.

useReducer(): Allows you to manage a complex state of a React component.

useParams() …..etc




- 🔸 Le Hook UseEffect():



The useEffect() Hook allows a React component to perform actions at specific times and when certain properties or state change. It is useful for carrying out side effects, such as:





  1. Data fetching

  2. Update the DOM

  3. Add or remove event listeners




The useEffect() hook takes 2 arguments: from a function and an array of dependencies :




CODE
useEffect(() => {
// Code to execute after rendering the component
// and after each update
}, [dependency]);








  • ✔️The function is executed after the component is rendered and after each update.

  • ✔️The dependency table allows you to specify the values ​​that should trigger execution of the function. If a value in the dependency array changes, the function is executed.




Example of using the useEffect() hook to update the DOM:




CODE
function App() {
const [count, setCount] useState(0);
useEffect(() => {
document.title = count;
). [count]);
return (
<div>
<h1>Count: [count]</h1>
<button onClick=(() => setCount(count + 1))>+1</button>
</div>
);)






In this example, the useEffect() hook is used to update the page title each time that the value of the count variable changes. This is because the count variable is included in the dependency table.



Example of using the useEffect() hook to fetch data:




CODE
function App() {
const [users, setUsers] = useState([]);
useEffect(()=>{
fetch('https://api.github.com/users')
.then((response) => response.json())
.then ((data) => setUsers(data))
}, []);
return (
<ul>
{[users.map((user) => (
<li key=(user.id)> [user.login)</li>
))}
</ul>
)}






In this example, the useEffect() hook is used to perform data fetching from the GitHub API . The function is executed only once , after the initial rendering of the component. That is due to the dependency table being empty [ ] .





  • ✔️Hooks are a powerful way to manage state and side effects in React components.

  • ✔️The useEffect() hook is particularly useful for performing asynchronous operations, such as data fetching.




Example: Add or remove event-listeners

It is possible to add or remove event listeners in a React component by using the useEffect() hook.



To add an event listener, simply use the addEventListener() function in the function of the useEffect() hook.



The addEventListener() function takes two arguments: the event to listen for and the function to callback that will be called when the event occurs.




CODE
window.removeEventListener(‘click’, handlclick]);







Here's an example of adding an event listener for a button click:




CODE
function App() {
const [count, setCount] = useState(0);
useEffect (() => {
document.getElementById('button').addEventListener('click',
() => {
setCount(count + 1);
});
}, []);
return (
<div>
<h1>Count: {count}</h1>
<button id="button">+1</button>
</div>
);
}


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
2 Quellen
Microsoft Phone Link Not Showing Messages on Windows 11? Fix It
1 Quelle
How to Enable Windows 11 Screen Savers
1 Quelle
Post-DEF CON phishing campaign delivered AMOS and NetSupport malware
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Hooks in React

Thematisch verwandte Begriffe: Hooks, React · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...