📰 IT NachrichtenWhy It’s Difficult for Tech Companies to Rein In A.I.(12.09.2026 um 11:02 Uhr)
🔧 AI Nachrichten Etzioni on AI: What kids tell chatbots, but not you(04.09.2026 um 16:05 Uhr)
🔧 AI Nachrichten OpenAI Wants to Know if an AI Industry Slowdown Would Even Be Legal(11.09.2026 um 01:28 Uhr)
🔧 AI Nachrichten OpenAI puts Pro subscriptions on hold due to Astra demand(10.09.2026 um 22:59 Uhr)
📰 IT NachrichtenWhy It’s Difficult for Tech Companies to Rein In A.I.(12.09.2026 um 11:02 Uhr)
🔧 AI Nachrichten Etzioni on AI: What kids tell chatbots, but not you(04.09.2026 um 16:05 Uhr)
🔧 AI Nachrichten OpenAI Wants to Know if an AI Industry Slowdown Would Even Be Legal(11.09.2026 um 01:28 Uhr)
🔧 AI Nachrichten OpenAI puts Pro subscriptions on hold due to Astra demand(10.09.2026 um 22:59 Uhr)

🔧 Programmierung 🕛 vor 2 Jahren 2 Min Lesezeit
0

useRef

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




What is useRef?



The useRef hook in React is used to access and interact with DOM elements directly or to keep a mutable value that does not cause re-renders when changed. It provides a way to persist values across renders without triggering a component update.






How useRef Works



useRef returns a mutable object called a "ref object" with a .current property. This property can hold a value that persists across re-renders.






Basic Syntax






CODE
const myRef = useRef(initialValue);








  • initialValue: The initial value for the ref object. This is optional.






Example 1: Accessing DOM Elements



In this example, we'll use useRef to access a DOM element directly.





  1. Create the Component




CODE
   import React, { useRef, useEffect } from 'react';

const FocusInput = () => {
// Create a ref with useRef
const inputRef = useRef(null);

useEffect(() => {
// Focus the input field when the component mounts
inputRef.current.focus();
}, []);

return (
<div>
<input ref={inputRef} type="text" placeholder="Focus me on mount" />
</div>
);
};

export default FocusInput;






Explanation:





  • useRef(null) creates a ref object.


  • inputRef.current gives access to the input element.


  • useEffect is used to focus the input field when the component mounts.






Example 2: Keeping Mutable Values



In this example, we'll use useRef to keep a mutable value that does not cause re-renders when updated.





  1. Create the Component




CODE
   import React, { useRef, useState } from 'react';

const Timer = () => {
const [count, setCount] = useState(0);
const timerRef = useRef(null);

const startTimer = () => {
if (timerRef.current) {
clearInterval(timerRef.current);
}
timerRef.current = setInterval(() => {
setCount(prevCount => prevCount + 1);
}, 1000);
};

const stopTimer = () => {
if (timerRef.current) {
clearInterval(timerRef.current);
}
};

return (
<div>
<h1>Time: {count}s</h1>
<button onClick={startTimer}>Start Timer</button>
<button onClick={stopTimer}>Stop Timer</button>
</div>
);
};

export default Timer;






Explanation:





  • timerRef.current is used to store the timer ID.


  • startTimer and stopTimer functions use timerRef.current to manage the timer.

  • Updating timerRef.current does not cause a re-render of the component.






Summary





  • useRef creates a mutable ref object with a .current property.

  • Use useRef to access DOM elements directly.

  • Use useRef to keep mutable values that do not cause re-renders.



That’s it! useRef is a powerful and versatile hook that’s useful in many scenarios.

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
5 Quellen
Rockwell Automation FactoryTalk Activation Manager
3 Quellen
September-Patchday: Adobe schließt kritische Zero-Day-Lücke und 172 weitere
2 Quellen
Jetzt patchen! Angreifer attackieren JFrog Artifactory und machen sich zu Admins
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten useRef

Thematisch verwandte Begriffe: useRef · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...