🧠 What are Refs in Class Components?
In class components, Refs are used to directly access a DOM element or a child component — just like in functional components, but the syntax is different.
🧩 Steps to Use Refs in Class Components
1️⃣ Create the Ref in the constructor
this.inputRef = React.createRef();2️⃣ Attach the Ref to a DOM element
<input type="text" ref={this.inputRef} />3️⃣ Use the Ref (e.g., to focus the input)
this.inputRef.current.focus();📦 Full Example
<div class="infographic-refsclass" id="rc">
<style>
.infographic-refsclass {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--line: #ddd6c9;
--accent: #2563eb;
--radius: 8px;
--mono-font: 'IBM Plex Mono', monospace;
--body-font: 'Inter', system-ui, sans-serif;
max-width: 820px;
margin: 0 auto;
box-sizing: border-box;
background: transparent;
color: var(--ink);
font-family: var(--body-font);
}
.infographic-refsclass[data-theme="dark"] {
--ink: #e8e6e1;
--ink-soft: #9a968c;
--paper: #13171f;
--line: #2c313d;
--accent: #5b8def;
}
.infographic-refsclass * { box-sizing: border-box; min-width: 0; }
.rc-grid { display: flex; flex-direction: column; gap: 1.3rem; align-items: stretch; }
.rc-code-block pre {
margin: 0; background: #0d0d10; border: 1px solid #24252d; border-radius: var(--radius);
padding: 0.9rem; overflow: auto; max-height: 380px;
}
.rc-code-block code { font-family: var(--mono-font); font-size: 0.76rem; line-height: 1.75; color: #d4d4d8; white-space: pre; }
.tok-kw { color: #c678dd; }
.tok-tag { color: #e06c75; }
.tok-type { color: #61afef; }
.tok-attr { color: #d19a66; }
.tok-str { color: #98c379; }
.tok-punc { color: #6b7280; }
.tok-comment { color: #7f848e; font-style: italic; }
.rc-highlight { background: rgba(34,197,94,0.35); border-radius: 3px; padding: 0 3px; margin: 0 -3px; transition: background 1.1s ease; }
.rc-output-label {
font-family: var(--mono-font); font-size: 0.68rem; letter-spacing: 0.1em; text-transform: uppercase;
color: var(--ink-soft); text-align: center;
}
.rc-preview { border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; box-shadow: 0 10px 24px -12px rgba(26,26,26,0.18); }
.rc-preview-bar { background: #e9e9e9; padding: 0.45rem 0.6rem; display: flex; gap: 0.3rem; }
.rc-preview-bar span { width: 8px; height: 8px; border-radius: 50%; display: block; }
.rc-preview-bar span:nth-child(1) { background: #ff5f56; }
.rc-preview-bar span:nth-child(2) { background: #ffbd2e; }
.rc-preview-bar span:nth-child(3) { background: #27c93f; }
.rc-preview-page { background: #fff; padding: 1.6rem; font-family: var(--body-font); text-align: center; }
.rc-real-input {
font-family: var(--body-font); font-size: 0.9rem; padding: 0.5rem 0.7rem; border: 1px solid #ccc;
border-radius: 5px; color: #000; margin-right: 0.6rem; transition: box-shadow 0.2s, border-color 0.2s;
}
.rc-real-input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(37,99,235,0.25); }
.rc-real-btn {
font-family: var(--body-font); font-size: 0.85rem; font-weight: 600; color: #fff; background: var(--accent);
border: none; border-radius: 100px; padding: 0.55rem 1.1rem; cursor: pointer;
box-shadow: 0 6px 16px -4px rgba(37,99,235,0.45); transition: transform 0.15s;
}
.rc-real-btn:hover { transform: translateY(-1px); }
.rc-real-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) {
.infographic-refsclass *, .infographic-refsclass *::before, .infographic-refsclass *::after {
transition-duration: 0.001ms !important; animation-duration: 0.001ms !important;
}
}
</style>
<div class="rc-grid">
<div class="rc-code-block">
<pre><code id="rcCode"></code></pre>
</div>
<p class="rc-output-label">OUTPUT</p>
<div class="rc-preview">
<div class="rc-preview-bar"><span></span><span></span><span></span></div>
<div class="rc-preview-page">
<input class="rc-real-input" type="text" id="rcInput" />
<button class="rc-real-btn" id="rcBtn" type="button">Focus the Input</button>
</div>
</div>
</div>
</div>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=IBM+Plex+Mono:wght@400;500&display=swap" rel="stylesheet">
<script>
(function () {
var root = document.getElementById('rc');
if (!root) return;
var bodyAttr = document.body.getAttribute('data-darkmode');
var dark;
if (bodyAttr === 'true') dark = true;
else if (bodyAttr === 'false') dark = false;
else {
var match = document.cookie.match(/(?:^|;\s*)darkMode=([^;]+)/);
dark = match ? match[1] === 'true' : false;
}
if (dark) root.setAttribute('data-theme', 'dark');
window.addEventListener('message', function (e) {
if (e.data && e.data.type === 'darkMode') {
if (e.data.value) root.setAttribute('data-theme', 'dark');
else root.removeAttribute('data-theme');
}
});
var code =
'<span class="tok-kw">import</span> <span class="tok-type">React</span><span class="tok-punc">,</span> <span class="tok-punc">{</span> Component <span class="tok-punc">}</span> <span class="tok-kw">from</span> <span class="tok-str">\'react\'</span><span class="tok-punc">;</span>\n\n' +
'<span class="tok-kw">class</span> <span class="tok-type">MyClassComponent</span> <span class="tok-kw">extends</span> <span class="tok-type">Component</span> <span class="tok-punc">{</span>\n' +
' <span class="tok-kw">constructor</span><span class="tok-punc">(</span>props<span class="tok-punc">) {</span>\n' +
' super<span class="tok-punc">(</span>props<span class="tok-punc">);</span>\n' +
' <span class="rc-highlight" id="rcLineCreate"><span class="tok-kw">this</span>.inputRef <span class="tok-punc">=</span> <span class="tok-type">React</span><span class="tok-punc">.</span>createRef<span class="tok-punc">();</span></span> <span class="tok-comment">// Step 1: Create ref</span>\n' +
' <span class="tok-punc">}</span>\n\n' +
' handleClick <span class="tok-punc">=</span> <span class="tok-punc">() => {</span>\n' +
' <span class="rc-highlight" id="rcLineFocus"><span class="tok-kw">this</span>.inputRef.current.focus<span class="tok-punc">();</span></span> <span class="tok-comment">// Step 3: Use ref</span>\n' +
' <span class="tok-punc">};</span>\n\n' +
' render<span class="tok-punc">() {</span>\n' +
' <span class="tok-kw">return</span> <span class="tok-punc">(</span>\n' +
' <span class="tok-punc"><</span><span class="tok-tag">div</span><span class="tok-punc">></span>\n' +
' <span class="tok-comment">{/* Step 2: Attach ref */}</span>\n' +
' <span class="rc-highlight" id="rcLineInput"><span class="tok-punc"><</span><span class="tok-tag">input</span> <span class="tok-attr">type</span><span class="tok-punc">=</span><span class="tok-str">"text"</span> <span class="tok-attr">ref</span><span class="tok-punc">={</span><span class="tok-kw">this</span>.inputRef<span class="tok-punc">}</span> <span class="tok-punc">/></span></span>\n' +
' <span class="rc-highlight" id="rcLineButton"><span class="tok-punc"><</span><span class="tok-tag">button</span> <span class="tok-attr">onClick</span><span class="tok-punc">={</span><span class="tok-kw">this</span>.handleClick<span class="tok-punc">}></span>Focus the Input<span class="tok-punc"></</span><span class="tok-tag">button</span><span class="tok-punc">></span></span>\n' +
' <span class="tok-punc"></</span><span class="tok-tag">div</span><span class="tok-punc">></span>\n' +
' <span class="tok-punc">);</span>\n' +
' <span class="tok-punc">}</span>\n' +
'<span class="tok-punc">}</span>\n\n' +
'<span class="tok-kw">export</span> <span class="tok-kw">default</span> <span class="tok-type">MyClassComponent</span><span class="tok-punc">;</span>';
root.querySelector('#rcCode').innerHTML = code;
var input = root.querySelector('#rcInput');
var btn = root.querySelector('#rcBtn');
var lineIds = ['rcLineCreate', 'rcLineFocus', 'rcLineInput', 'rcLineButton'];
function flashLines() {
lineIds.forEach(function (id) {
var el = root.querySelector('#' + id);
if (!el) return;
el.style.background = 'rgba(34,197,94,0.35)';
void el.offsetWidth;
el.style.background = 'transparent';
});
}
btn.addEventListener('click', function () {
input.focus(); // this is the real, actual behavior — this.inputRef.current.focus()
flashLines();
});
})();
</script>✅ Explanation of the Class Component with Refs
class MyClassComponent extends Component→ Creates a class-based React component
constructor(props)→ Initializes the component and prepares refs
super(props)→ Passes
propsto the parentComponentclassthis.inputRef = React.createRef()→ Creates a ref to directly access the input element
ref={this.inputRef}→ Attaches the ref to the
<input>elementhandleClick = () => { ... }→ Arrow function keeps
thisautomatically boundthis.inputRef.current→ Points to the actual input DOM element
.focus()→ Moves the cursor into the input box
<button onClick={this.handleClick}>→ Clicking the button focuses the input field
🔁 Summary Table
Step | Code |
|---|---|
Create Ref |
|
Attach Ref |
|
Use Ref |
|