🔧 Programmierung 🕛 vor 3 Monaten 2 Min Lesezeit
0

useRef Hook in react

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




useRef :



The useRef Hook allows you to persist values between renders.



It can be used to store a mutable value that does not cause a re-render when updated.



It can be used to access a DOM element directly.






Syntax :






CODE
import { useRef } from 'react';

const myRef = useRef(initialValue);










Important Points :



When you call useRef, it returns a plain JavaScript object with a single property called current.



The most important useRef is that changing its value does not trigger a component re-render.






Example Code :






CODE
import React from 'react'

import { useState, useRef } from "react";

function UserRef() {

let normalVar = 5;

const myRef = useRef(5);

const [count, setCount] = useState(5);

return (
<div>
<p>Normal Variable: {normalVar}</p>
<p>Ref Variable: {myRef.current}</p>
<p>State Variable: {count}</p>

{/* Increments value in memory, but UI does not change */}
<button onClick={() => normalVar++}>
Increment Normal
</button>

{/* Increments ref.current, but UI does not change */}
<button onClick={() => myRef.current = myRef.current + 1}>
Increment Ref
</button>

{/* Increments state and forces the component to re-render, displaying new values */}
<button onClick={() => setCount(count + 1)}>
Increment State
</button>
</div>
);
}
export default UserRef;










Explanation :



First you click a normal variable button the value change only memory but, dosen't change the UI.



Then click the useRef variable button two time it's also dosen't change UI.



Then click the state variable button it changes and updates the values both useRef and state variables.



Because ,the useRef remembers the values and shows the values when the component re-renders.



The useRef button clicked by two times so, it's updates 7 and state variable clicked by one time it shows the value 6.






Output :



Vollständiger Original-Artikel
Den kompletten Beitrag mit allen Details direkt auf dev.to lesen.
↗ 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
11 Quellen
CVE-2026-16794 | GitLab up to 19.1.7/19.2.5/19.3.1 Compliance Framework Management improper authorization (WID-SEC-2026-3315)
1 Quelle
Windows 11: Auto-Update-Installation, aber keine Einträge in Verlauf? - BornCity
1 Quelle
CVE-2026-76438 | Cisco BroadWorks Web-based Management Interface improper authorization (EUVD-2026-81161)
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten useRef Hook in react

Thematisch verwandte Begriffe: useRef, Hook, 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 ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...