ရိုးရိုးလေး ရှင်းပြမယ်
Ref ကို ရိုးရိုးလေး ပြောရရင်
Ref ဆိုတာ component ထဲမှာ ထားတဲ့ အံဆွဲလေး တစ်ခုလိုပါပဲ။ အထဲကို ဘာမဆို ထည့်ထားလို့ ရပြီး render တိုင်း ပျောက်မသွားပါဘူး၊ ဒါပေမဲ့ အထဲက ပစ္စည်းကို လဲလိုက်တိုင်း React က ဘာမှ သတိမထားပါဘူး — screen ကို ပြန်မဆွဲပါဘူး။ State ကတော့ ဆန့်ကျင်ဘက်ပါ: state ပြောင်းတိုင်း React က component ကို ပြန် render လုပ်ပါတယ်။
Ref ကို အဓိကအားဖြင့် နေရာနှစ်ခုမှာ သုံးပါတယ်။ ပထမက React က ဖန်တီးပေးထားတဲ့ input, video လို DOM element အစစ်ကို ကိုင်ဖို့ပါ၊ ဥပမာ button နှိပ်ရင် search box ထဲ cursor ရောက်သွားအောင် focus လုပ်တာမျိုးပါ။ ဒုတိယကတော့ timer id လို screen ပေါ် မပြတဲ့ value တွေကို render တွေကြား မှတ်ထားဖို့ပါ။
Problem → Why → Solution
ပြဿနာ: ရပ်လို့ မရတဲ့ stopwatch
Start နဲ့ Stop button ပါတဲ့ stopwatch လေး တစ်ခု ရေးတယ် ဆိုပါစို့။ Interval id ကို နောက်မှ clearInterval လုပ်ဖို့ ရိုးရိုး let variable ထဲ သိမ်းထားလိုက်ပါတယ်။ Start နှိပ်တော့ စက္ကန့်တွေ တက်လာပါတယ်၊ ဒါပေမဲ့ Stop နှိပ်လိုက်တော့ ဘာမှ မဖြစ်ပါဘူး — နံပါတ်တွေ ဆက်တက်နေပါတယ်။ Console မှာ error လည်း တစ်ခုမှ မပေါ်ပါဘူး။
function Stopwatch() {
const [seconds, setSeconds] = useState(0);
let intervalId; // a fresh, empty variable on every render
function start() {
intervalId = setInterval(() => setSeconds((s) => s + 1), 1000);
}
function stop() {
clearInterval(intervalId); // this render's copy is still undefined
}
return (
<>
<p>{seconds}s</p>
<button onClick={start}>Start</button>
<button onClick={stop}>Stop</button>
</>
);
}ဘာကြောင့် Stop က အလုပ်မလုပ်တာလဲ
Component function ဟာ render တိုင်း အစကနေ ပြန် run ပါတယ်၊ ဒါကြောင့် let intervalId ကလည်း render တိုင်း အသစ် (undefined) ဖြစ်ပါတယ်။ Start က ပထမ render ရဲ့ variable ထဲကို id ထည့်ပေမဲ့ setSeconds က နောက် render ကို ဖြစ်စေပြီး Stop button က အဲဒီ render အသစ်ရဲ့ variable အလွတ်ကို clear လုပ်ပါတယ်။ id ကို state ထဲ ထည့်ရင်တော့ အလုပ်လုပ်ပေမဲ့ screen ပေါ် မပြတဲ့ value အတွက် မလိုအပ်တဲ့ render တစ်ခု ပိုဖြစ်ပါတယ်။ useRef ကတော့ render တွေကြား မပျောက်ဘဲ ပြောင်းလဲမှုကလည်း re-render မဖြစ်စေတဲ့ နေရာ အတိအကျကို ပေးပါတယ်။
Core concept: render တွေကြား ကျန်နေတဲ့ box တစ်ခု
useRef(initialValue) က { current: initialValue } ဆိုတဲ့ ရိုးရိုး object တစ်ခုကို ပြန်ပေးပါတယ်။ ပထမ render မှာ React က အဲဒီ object ကို ဖန်တီးပြီး နောက် render တိုင်းမှာ object တူတူကိုပဲ ပြန်ပေးပါတယ်။ ref.current ကို ဖတ်လို့ရသလို အသစ်လည်း assign လုပ်လို့ ရပါတယ်၊ React က အဲဒီ assignment ကို စောင့်ကြည့်မနေတဲ့အတွက် component ပြန် render မဖြစ်ပါဘူး။
Initial value ကို ပထမ render မှာပဲ သုံးပြီး နောက် render တွေမှာ လျစ်လျူရှုပါတယ်။ Component instance တစ်ခုချင်းစီက ကိုယ်ပိုင် ref ကို ရတဲ့အတွက် Stopwatch နှစ်ခု render လုပ်ရင် timer id နှစ်ခုကို သီးခြားစီ မှတ်ထားနိုင်ပါတယ် — component အပြင်ဘက်က module variable တစ်ခုနဲ့ဆိုရင် instance အားလုံး variable တစ်ခုတည်းကို မျှသုံးမိပါလိမ့်မယ်။
| useState | useRef | |
|---|---|---|
| ပြောင်းရင် re-render | ဖြစ်တယ် | မဖြစ်ဘူး |
| ပြောင်းနည်း | setter function နဲ့ | ref.current ကို တိုက်ရိုက် assign |
| Render အတွင်း ဖတ်လို့ ရလား | ရတယ်၊ UI အတွက် ဖြစ်တယ် | မဖတ်သင့်ဘူး၊ handler နဲ့ effect ထဲမှာ ဖတ်ပါ |
| သုံးသင့်တဲ့ နေရာ | screen ပေါ်မှာ ပြရမယ့် data | DOM node, timer id, အရင် value |
JSX element တစ်ခုမှာ ref={inputRef} လို့ ရေးလိုက်ရင် React က DOM node ကို ဖန်တီးပြီး screen ပေါ် commit လုပ်ပြီးချိန်မှာ inputRef.current ထဲကို အဲဒီ node ကို ထည့်ပေးပါတယ်။ Element ဖယ်ရှားခံရရင် current ကို null ပြန်ထားပေးပါတယ်။ ဒါကြောင့် ပထမ render လုပ်နေစဉ်မှာ current က null ပဲ ရှိသေးပါတယ် — node ကို event handler ဒါမှမဟုတ် effect ထဲမှာမှ သုံးရပါမယ်၊ အဲဒီအချိန်ရောက်ရင် screen ပေါ်မှာ ရှိနေပါပြီ။
Ref တွေကို 'escape hatch' လို့ ခေါ်ပါတယ်။ React ရဲ့ ပုံမှန် data flow (state ပြောင်း -> UI ပြန်ဆွဲ) အပြင်ဘက်ကို ထွက်ခွင့်ပေးလို့ပါ။ Focus, scroll, text ရွေးခြင်း၊ video play/pause လို browser API တွေက JSX နဲ့ ဖော်ပြလို့ မရတဲ့ လုပ်ဆောင်ချက်တွေ ဖြစ်တဲ့အတွက် ref နဲ့ လုပ်ရတာ သဘာဝကျပါတယ်။ ဒါပေမဲ့ React ကိုယ်တိုင် စီမံနေတဲ့ text, class, child element တွေကို ref ကနေ ဝင်ပြင်ရင် React ရဲ့ မှတ်တမ်းနဲ့ DOM မကိုက်တော့ပါဘူး၊ အဲဒါတွေကိုတော့ state နဲ့ပဲ ပြောင်းပါ။
- useRef က object တစ်ခုတည်းကို ပေး
- React က commit ပြီးမှ node ကို ထည့်
- Handler/effect ထဲမှာ current ကို သုံး
- current ပြောင်းလည်း re-render မဖြစ်
Screen ပေါ် ပြရမယ့် value ဖြစ်ရင် state ကို သုံးပါ။ Ref က UI ကို မမောင်းတဲ့ value တွေအတွက်ပဲ ဖြစ်ပါတယ်။
အထွေထွေ ပုံစံ
const someRef = useRef(initialValue);
<element ref={someRef} /> // React sets someRef.current after commit
function handleEvent() {
someRef.current.someMethod(); // read or write in handlers/effects
}
// React 19: your own function component receives ref as a prop
function Child({ ref, ...props }) {
return <element ref={ref} {...props} />;
}Ref ကို component ရဲ့ top level မှာ ဖန်တီးပါ၊ DOM node လိုရင် JSX element ရဲ့ ref attribute ကို ပေးပါ၊ current ကိုတော့ render အပြင်ဘက် — handler နဲ့ effect ထဲမှာပဲ ကိုင်ပါ။
Worked example: button နဲ့ search box ကို focus လုပ်ခြင်း
Task list app တွေမှာ 'Focus search' button လေး နှိပ်ရင် keyboard cursor က search input ထဲ တန်းရောက်သွားတာ အသုံးဝင်ပါတယ်။ Focus ဆိုတာ state နဲ့ ဖော်ပြလို့ မရတဲ့ browser action ဖြစ်တဲ့အတွက် DOM node ကို ref နဲ့ ယူပြီး focus() ကို တိုက်ရိုက် ခေါ်ရပါတယ်။
import { useRef } from 'react';
export default function SearchBox() {
const inputRef = useRef(null);
return <>
<input ref={inputRef} aria-label="Search tasks" />
<button onClick={() => inputRef.current?.focus()}>Focus search</button>
</>;
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Create the ref
const inputRef = useRef(null);Node မရှိသေးလို့ null နဲ့ စပါတယ်။ inputRef object က render တိုင်း တူတူပဲ ဖြစ်ပါတယ်။
- Attach it to the input
<input ref={inputRef} aria-label="Search tasks" />Input ကို screen ပေါ် ထည့်ပြီးတာနဲ့ React က inputRef.current ထဲကို input DOM node ကို ထည့်ပေးပါတယ်။ aria-label ကြောင့် screen reader သုံးသူတွေလည်း field ရဲ့ အဓိပ္ပာယ်ကို ကြားရပါတယ်။
- Use the node in a handler
<button onClick={() => inputRef.current?.focus()}>Focus search</button>Click ဖြစ်တဲ့အချိန်မှာ node ရှိပြီးသားပါ၊ ဒါကြောင့် browser ရဲ့ focus() method ကို ခေါ်လို့ ရပါတယ်။ ?. က input မရှိတော့တဲ့ အခြေအနေမျိုး (null) မှာ crash မဖြစ်အောင် ကာကွယ်ပေးပါတယ်။
- Fragment wrapper
return <><> ... </> fragment က wrapper div အပို မထည့်ဘဲ input နဲ့ button ကို sibling အဖြစ် ပြန်ပေးပါတယ်။
Button နှိပ်လျှင် keyboard focus က search input သို့ရွှေ့မည်။ကိုယ်ပိုင် component ဆီ ref ပေးခြင်း (React 19)
Input ကို TextField လို ကိုယ်ပိုင် component ထဲ ထုပ်ထားရင် parent က input ကို ဘယ်လို focus လုပ်မလဲ? React 19 မတိုင်ခင်က function component တွေက ref ကို prop အဖြစ် မရနိုင်လို့ forwardRef နဲ့ ထုပ်ရပါတယ်။ React 19 မှာတော့ function component က ref ကို တခြား prop တွေလိုပဲ လက်ခံပြီး အတွင်းက element ဆီ ဆက်ပေးလို့ ရပါပြီ။ forwardRef က ဆက်အလုပ်လုပ်နေဆဲပါ၊ ဒါပေမဲ့ code အသစ်မှာ မလိုတော့ပါဘူး။
import { useRef } from 'react';
function TextField({ label, ref }) {
return (
<label>
{label}
<input ref={ref} />
</label>
);
}
export default function SignupForm() {
const emailRef = useRef(null);
return (
<>
<TextField label="Email" ref={emailRef} />
<button onClick={() => emailRef.current.focus()}>Fix email</button>
</>
);
}Parent က အတွင်းက DOM node တစ်ခုလုံးကို မပေးချင်ဘဲ focus လို method အနည်းငယ်ကိုပဲ ထုတ်ပေးချင်ရင် useImperativeHandle ကို သုံးလို့ ရပါတယ်။ ဒါ့အပြင် ref attribute က function တစ်ခုကိုလည်း လက်ခံပါတယ် (ref callback)၊ React 19 မှာ အဲဒီ function ကနေ cleanup function ကို return ပြန်လို့ ရပြီး element ဖယ်ရှားခံရချိန်မှာ React က ခေါ်ပေးပါတယ်။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ ref ကို ဘယ်မှာ သုံးလဲ
Production app တွေမှာ ref အများစုက accessibility နဲ့ browser API တွေနဲ့ ဆက်စပ်ပါတယ်။ Keyboard နဲ့ပဲ သုံးနေတဲ့သူအတွက် focus ကို မှန်ကန်တဲ့ နေရာ ရွှေ့ပေးတာက ref မပါဘဲ မဖြစ်နိုင်ပါဘူး။
- Form validation: submit မအောင်မြင်ရင် ပထမဆုံး မှားနေတဲ့ field ကို focus လုပ်ပေးခြင်း၊ ဒါမှ keyboard နဲ့ screen reader သုံးသူတွေ error ကို ချက်ချင်း သိရပါတယ်။
- Modal dialog: ဖွင့်ချိန်မှာ dialog ထဲက ပထမ button ကို focus ပေးပြီး ပိတ်ချိန်မှာ ဖွင့်ခဲ့တဲ့ button ဆီ focus ပြန်ပို့ခြင်း။
- Chat app: message အသစ်ရောက်ရင် စာရင်းရဲ့ အောက်ဆုံးကို scrollIntoView နဲ့ scroll ဆွဲခြင်း။
- Media နဲ့ third-party library: video.play(), canvas drawing, chart ဒါမှမဟုတ် map library တွေကို container DOM node ပေးခြင်း။
- DOM မဟုတ်တဲ့ value တွေ: timer id, WebSocket instance, input ရဲ့ အရင် value တို့ကို re-render မဖြစ်စေဘဲ သိမ်းထားခြင်း။
အမှားများ
Ref နဲ့ ဖြစ်လေ့ရှိတဲ့ အမှားများ
Render လုပ်နေစဉ် ref.current ကို ဖတ်ခြင်း
Component body ထဲမှာ inputRef.current.value လို့ ရေးရင် ပထမ render မှာ current က null ဖြစ်နေသေးလို့ TypeError တက်ပါတယ်။ နောက် render တွေမှာ အလုပ်လုပ်သလို ထင်ရပေမဲ့ တန်ဖိုးက နောက်ကျနေတတ်ပါတယ်။ Node ကို handler ဒါမှမဟုတ် effect ထဲမှာပဲ ကိုင်ပါ။
Ref ပြောင်းရင် screen update ဖြစ်မယ်လို့ ထင်ခြင်း
clicksRef.current += 1 လို့ လုပ်ပြီး <p>{clicksRef.current}</p> က ပြောင်းမယ်လို့ စောင့်နေရင် မပြောင်းပါဘူး — React က ref ပြောင်းတာကို မသိလို့ပါ။ တခြား အကြောင်းတစ်ခုကြောင့် render ဖြစ်တဲ့အခါကျမှ နံပါတ် ရုတ်တရက် ခုန်တက်သွားတတ်ပါတယ်။ Screen ပေါ် ပြရမယ့် value ဆိုရင် state ကို သုံးပါ။
React စီမံနေတဲ့ DOM ကို ref နဲ့ ဝင်ပြင်ခြင်း
ref.current.textContent = '...' ဒါမှမဟုတ် ref.current.remove() လို code က React ရဲ့ မှတ်တမ်းနဲ့ DOM ကို မကိုက်ညီစေပါဘူး။ နောက် render မှာ React က သင့်ပြင်ဆင်မှုကို ဖျက်ပစ်နိုင်သလို 'node not found' လို error တွေလည်း တက်နိုင်ပါတယ်။ Focus, scroll, measure လို React မစီမံတဲ့ action တွေအတွက်ပဲ ref ကို သုံးပါ။
Mini challenge
Mini challenge: ရပ်လို့ရတဲ့ stopwatch
Problem section က ပျက်နေတဲ့ stopwatch ကို ref သုံးပြီး ပြင်ပါ။ Start, Stop, Reset button သုံးခု ပါရမယ်၊ Start ကို နှစ်ခါ ဆက်တိုက် နှိပ်လည်း timer နှစ်ခု မဖြစ်ရပါဘူး၊ component ဖယ်ရှားခံရရင် timer ရပ်ရပါမယ်။
- Elapsed seconds ကို state ထဲမှာ ထားပါ (screen ပေါ် ပြလို့)
- Interval id ကို useRef ထဲမှာ သိမ်းပြီး မ run နေရင် null ထားပါ
- Start က timer run နေပြီးသားဆိုရင် ဘာမှ မလုပ်ရပါ
- Reset က timer ကို ရပ်ပြီး 0 ပြန်ထားရပါမယ်၊ unmount ချိန်မှာ effect cleanup နဲ့ timer ကို ရှင်းပါ
import { useEffect, useRef, useState } from 'react';
export default function Stopwatch() {
// TODO: elapsed seconds in state, interval id in a ref (start with null)
function start() {
// TODO: return early if already running, otherwise start an interval
}
function stop() {
// TODO: clear the interval and mark the ref as not running
}
function reset() {
// TODO: stop, then put the seconds back to 0
}
// TODO: useEffect with a cleanup that clears the interval on unmount
return null; // TODO: show "Elapsed: Ns" and the three buttons
}ရလဒ်: Start နှိပ်ရင် Elapsed: 1s, 2s ... တက်လာပြီး Start ကို ထပ်နှိပ်လည်း မြန်မလာပါဘူး။ Stop နှိပ်ရင် နံပါတ် ရပ်သွားပြီး Start ပြန်နှိပ်ရင် ရပ်ခဲ့တဲ့ နေရာကနေ ဆက်တက်ပါတယ်။ Reset နှိပ်ရင် Elapsed: 0s ပြန်ဖြစ်ပြီး ဆက်မတက်တော့ပါဘူး။
အရိပ်အမြွက် ကြည့်ရန်
intervalRef.current !== null ဆိုရင် timer run နေပါပြီ။ Stop ထဲမှာ clearInterval လုပ်ပြီးတိုင်း current ကို null ပြန်ထားမှ Start ကို ထပ်နှိပ်လို့ ရပါမယ်။ setElapsed((s) => s + 1) လို updater form က interval callback ထဲမှာ နောက်ဆုံး value ကို အမြဲ သုံးပေးပါတယ်။
အဖြေ ကြည့်ရန်
import { useEffect, useRef, useState } from 'react';
export default function Stopwatch() {
const [elapsed, setElapsed] = useState(0);
const intervalRef = useRef(null);
function start() {
if (intervalRef.current !== null) return; // already running
intervalRef.current = setInterval(() => {
setElapsed((s) => s + 1);
}, 1000);
}
function stop() {
clearInterval(intervalRef.current);
intervalRef.current = null;
}
function reset() {
stop();
setElapsed(0);
}
useEffect(() => {
return () => clearInterval(intervalRef.current); // stop on unmount
}, []);
return (
<div>
<p>Elapsed: {elapsed}s</p>
<button onClick={start}>Start</button>
<button onClick={stop}>Stop</button>
<button onClick={reset}>Reset</button>
</div>
);
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- useRef က render တိုင်း object တူတူကို ပြန်ပေးပြီး ref.current ပြောင်းလည်း re-render မဖြစ်ပါဘူး
- ref={myRef} ပေးထားရင် React က commit ပြီးမှ DOM node ကို current ထဲ ထည့်ပြီး ဖယ်ရှားရင် null ပြန်ထားပါတယ်
- current ကို handler နဲ့ effect ထဲမှာပဲ ကိုင်ပါ၊ screen ပေါ်ပြရမယ့် value ဆိုရင် state ကို သုံးပါ
- Focus, scroll, media, timer id တွေအတွက် ref ကို သုံးပြီး React စီမံတဲ့ DOM ကိုတော့ မပြင်ပါနဲ့
- React 19 မှာ function component က ref ကို prop အဖြစ် ယူနိုင်လို့ forwardRef မလိုတော့ပါဘူး
Manipulating the DOM with Refs — React