ရိုးရိုးလေး ရှင်းပြမယ်
Custom hook ကို ရိုးရိုးလေး ပြောရရင်
Custom hook ဆိုတာ use နဲ့ စတဲ့ နာမည်ရှိပြီး useState, useEffect လို တခြား hook တွေကို အထဲမှာ ခေါ်သုံးထားတဲ့ ရိုးရိုး JavaScript function တစ်ခုပါ။ Component နှစ်ခု သုံးခုက 'window အကျယ်ကို စောင့်ကြည့်တာ' ဒါမှမဟုတ် 'internet ရှိမရှိ စစ်တာ' လို logic တူတူကို လိုအပ်တဲ့အခါ copy-paste လုပ်မယ့်အစား hook တစ်ခုထဲမှာ တစ်ကြိမ်ပဲ ရေးပါတယ်။
Recipe တစ်ခုလို စဉ်းစားကြည့်ပါ။ စားဖိုမှူး နှစ်ယောက်က recipe တူတူကို သုံးပေမဲ့ ဒယ်အိုး နှစ်လုံးမှာ သီးခြားစီ ချက်ကြပါတယ်။ Custom hook ကလည်း အဲလိုပါပဲ — ခေါ်တဲ့ component တိုင်းက လုပ်ထုံးလုပ်နည်း တူတူကို ရပေမဲ့ state ကတော့ ကိုယ်ပိုင် ဖြစ်ပါတယ်။
Problem → Why → Solution
ပြဿနာ: copy ကူးထားတဲ့ logic တွေ တဖြည်းဖြည်း ကွဲထွက်လာခြင်း
StatusBar က 'Online/Offline' ကို ပြပြီး SaveButton ကတော့ offline ဖြစ်နေရင် disable ဖြစ်ရပါမယ်။ ပထမ component မှာ listener နှစ်ခုနဲ့ cleanup ကို ဂရုတစိုက် ရေးခဲ့ပြီး ဒုတိယ component အတွက်တော့ အမြန် copy ကူးရင်း cleanup ကျန်ခဲ့ပါတယ်။ SaveButton ကို mount/unmount လုပ်တိုင်း listener တွေ စုပုံလာပြီး unmount ဖြစ်ပြီးသား component ရဲ့ setter ကိုလည်း ဆက်ခေါ်နေပါတော့တယ်။
function StatusBar() {
const [isOnline, setIsOnline] = useState(navigator.onLine);
useEffect(() => {
const on = () => setIsOnline(true);
const off = () => setIsOnline(false);
window.addEventListener('online', on);
window.addEventListener('offline', off);
return () => {
window.removeEventListener('online', on);
window.removeEventListener('offline', off);
};
}, []);
return <p>{isOnline ? 'Online' : 'Offline'}</p>;
}
function SaveButton() {
const [isOnline, setIsOnline] = useState(navigator.onLine);
useEffect(() => {
window.addEventListener('online', () => setIsOnline(true));
window.addEventListener('offline', () => setIsOnline(false));
// copied in a hurry: no cleanup, listeners pile up
}, []);
return <button disabled={!isOnline}>Save</button>;
}Copy-paste က ဘာကြောင့် အန္တရာယ်ရှိလဲ
Copy တစ်ခုစီက bug ပြင်ရမယ့် နေရာ တစ်ခုစီ ဖြစ်လာပါတယ်။ ပထမ copy မှာ ပြင်ခဲ့တဲ့ bug က ဒုတိယ copy မှာ ကျန်နေတတ်ပြီး component တွေ များလာလေ ဘယ် version မှန်လဲ ဘယ်သူမှ မသိတော့လေ ဖြစ်ပါတယ်။ Custom hook က logic ကို နေရာ တစ်ခုတည်းမှာ ထားပေးလို့ cleanup ကို တစ်ကြိမ် မှန်အောင် ရေးလိုက်တာနဲ့ သုံးတဲ့ component အားလုံး မှန်သွားပါတယ်။
Core concept: logic ကို မျှတယ်၊ state ကို မမျှဘူး
Custom hook ထဲမှာ ထူးဆန်းတဲ့ magic မရှိပါဘူး။ Component က useWindowWidth() ကို ခေါ်လိုက်ရင် hook ထဲက useState နဲ့ useEffect တွေဟာ အဲဒီ component ကိုယ်တိုင် တိုက်ရိုက် ခေါ်သလိုပဲ အလုပ်လုပ်ပါတယ်။ ဒါကြောင့် component နှစ်ခုက hook တူတူကို ခေါ်ရင် state နှစ်ခု၊ effect နှစ်ခု၊ listener နှစ်ခု သီးခြားစီ ရပါတယ်။ Component တွေကြား value တစ်ခုတည်းကို တကယ် မျှချင်ရင် state ကို parent ဆီ lift up လုပ်ရမယ်၊ ဒါမှမဟုတ် context ကို သုံးရပါမယ်။
Hook က ဘာကို return ပြန်မလဲဆိုတာ သင် ဆုံးဖြတ်ရပါတယ်။ Value တစ်ခုတည်းဆိုရင် (width လို) တိုက်ရိုက် ပြန်ပေးပါ။ Value နဲ့ action တစ်တွဲဆိုရင် useState လို [value, setValue] array ကို ပြန်ပေးနိုင်ပါတယ်၊ အဲဒါက ခေါ်သူကို နာမည်ကြိုက်သလို ပေးခွင့်ရစေပါတယ်။ အပိုင်း သုံးခု လေးခု ရှိရင် { data, error, isLoading } လို object က ဖတ်ရ ပိုလွယ်ပါတယ်။ Hook တွေကလည်း argument ယူနိုင်ပါတယ်၊ ဒါပေမဲ့ effect ထဲမှာ သုံးတဲ့ argument တိုင်းကို dependency array ထဲ ထည့်ရပါမယ်။
Rules of Hooks နှစ်ချက် ရှိပါတယ်: hook ကို component ဒါမှမဟုတ် တခြား hook ရဲ့ top level မှာပဲ ခေါ်ပါ (if, loop, early return နောက်မှာ မခေါ်ပါနဲ့)၊ ပြီးတော့ ရိုးရိုး JavaScript function ထဲကနေ မခေါ်ပါနဲ့။ React က component တစ်ခုရဲ့ hook state တွေကို နာမည်နဲ့ မဟုတ်ဘဲ ခေါ်တဲ့ အစီအစဉ်နဲ့ မှတ်ထားလို့ပါ — render တစ်ခုမှာ hook တစ်ခု ကျော်သွားရင် နောက်က state အားလုံး နေရာလွဲသွားပါတယ်။ use prefix က လိုက်နာဖို့ convention ပါ၊ eslint-plugin-react-hooks က အဲဒီ နာမည်ကို ကြည့်ပြီး ဒီစည်းမျဉ်းတွေကို စစ်ပေးပါတယ်။
ဘယ်အချိန် hook ထုတ်သင့်လဲ? Component နှစ်ခုထက် ပိုပြီး state နဲ့ effect အတွဲ တူတူ ထပ်နေတဲ့အခါ၊ ဒါမှမဟုတ် component တစ်ခုတည်းမှာတောင် 'ဒါက ဘာလုပ်တာလဲ' ဆိုတာ နာမည်တစ်ခုနဲ့ ဖော်ပြချင်တဲ့အခါ ထုတ်ပါ။ useOnlineStatus လို နာမည်က useEffect ငါးကြောင်းထက် ရည်ရွယ်ချက်ကို ပိုရှင်းရှင်း ပြောပြပါတယ်။ Hook မပါတဲ့ အကူ function တစ်ခုကိုတော့ hook အဖြစ် မပြောင်းပါနဲ့။
- Component က useX() ကို top level မှာ ခေါ်
- Hook ထဲက state/effect တွေ အဲဒီမှာ run
- Component တစ်ခုစီက state ကိုယ်ပိုင် ရ
- Hook က value ကို ပြန်ပေး၊ UI က သုံး
Function ထဲမှာ hook တစ်ခုမှ မခေါ်ရင် အဲဒါက hook မဟုတ်ပါဘူး — use မပါတဲ့ ရိုးရိုး function နာမည် ပေးပါ။
အထွေထွေ ပုံစံ
function useSomething(arg) {
const [value, setValue] = useState(initialValue);
useEffect(() => {
// subscribe or start work that depends on arg
return () => { /* undo it */ };
}, [arg]);
return value; // or [value, action] or { value, action }
}
function SomeComponent() {
const value = useSomething(arg); // top level only, never inside if/loops
}Hook နာမည်ကို use နဲ့ စပါ၊ အထဲမှာ တခြား hook တွေကို ခေါ်ပါ၊ effect ထဲမှာ သုံးတဲ့ argument တွေကို dependency ထဲ ထည့်ပါ၊ component က လိုအပ်တာကိုပဲ return ပြန်ပါ။
Worked example: useWindowWidth
အောက်က hook က browser window ရဲ့ အကျယ်ကို state ထဲ ထားပြီး resize event ဖြစ်တိုင်း update လုပ်ပါတယ်။ App component ကတော့ resize listener အကြောင်း ဘာမှ မသိဘဲ hook ကို ခေါ်ပြီး ပြန်ရလာတဲ့ နံပါတ်ကို ပြရုံပါပဲ။
Server rendering သတိပေးချက်
useState(window.innerWidth) က render လုပ်နေစဉ် window ကို ဖတ်ပါတယ်။ Vite နဲ့ ဆောက်တဲ့ browser-only app မှာ ပြဿနာ မရှိပေမဲ့ server ပေါ်မှာ render လုပ်တဲ့ framework တွေမှာ window မရှိလို့ crash ဖြစ်ပါတယ်။ အဲဒီလို app မျိုးမှာ fallback value နဲ့ စပြီး effect ထဲမှာမှ window ကို ဖတ်ပါ။
import { useEffect, useState } from 'react';
function useWindowWidth() {
const [width, setWidth] = useState(window.innerWidth);
useEffect(() => {
function handleResize() {
setWidth(window.innerWidth);
}
window.addEventListener('resize', handleResize);
return () => window.removeEventListener('resize', handleResize);
}, []);
return width;
}
function App() {
const width = useWindowWidth();
return <p>Window width is {width}px</p>;
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- A function named use...
function useWindowWidth() {use prefix က React နဲ့ linter ကို ဒါ hook ဖြစ်ကြောင်း ပြောပြပါတယ်။ ကျန်တာကတော့ ရိုးရိုး function တစ်ခုပါပဲ။
- State owned by the caller
const [width, setWidth] = useState(window.innerWidth);ဒီ state က hook ကို ခေါ်တဲ့ component ပိုင်ပါတယ်။ Component နှစ်ခုက ခေါ်ရင် width state နှစ်ခု သီးခြားစီ ဖြစ်ပါတယ်။
- Subscribe once
window.addEventListener('resize', handleResize);Dependency array က [] ဖြစ်လို့ component mount ဖြစ်ချိန်မှာ listener ကို တစ်ကြိမ် ထည့်ပါတယ်။ Resize တိုင်း setWidth က re-render ကို ဖြစ်စေပါတယ်။
- Clean up
return () => window.removeEventListener('resize', handleResize);Component unmount ဖြစ်ရင် (development မှာ Strict Mode ရဲ့ စမ်းသပ် remount ချိန်မှာလည်း) listener ကို ဖယ်ရှားပါတယ်။ Function reference တူတူ ဖြစ်ရပါမယ်၊ ဒါကြောင့် handleResize ကို နာမည်ပေးထားတာပါ။
- Use it like a built-in hook
const width = useWindowWidth();App က top level မှာ hook ကို ခေါ်ပြီး number တစ်ခု ပြန်ရပါတယ်။ Listener အသေးစိတ်တွေ အားလုံး hook ထဲမှာ ဝှက်ထားပါတယ်။
App ကို render လုပ်ရင် 'Window width is 1280px' လို စာကြောင်း တစ်ကြောင်း ပေါ်ပါတယ် (နံပါတ်က သင့် window အကျယ်ပေါ် မူတည်ပါတယ်)။ Window ကို resize လုပ်တိုင်း နံပါတ် update ဖြစ်ပါတယ်။ပြဿနာကို hook တစ်ခုနဲ့ ပြင်ခြင်း
Problem section က component နှစ်ခုကို useOnlineStatus hook တစ်ခုနဲ့ ပြန်ရေးလိုက်ရင် cleanup ပါတဲ့ မှန်ကန်တဲ့ version တစ်ခုတည်း ကျန်ပါတယ်။ Component တစ်ခုစီက isOnline boolean တစ်ခုကိုပဲ ရပြီး ကိုယ်လိုသလို UI ဆွဲပါတယ်။
function useOnlineStatus() {
const [isOnline, setIsOnline] = useState(navigator.onLine);
useEffect(() => {
const goOnline = () => setIsOnline(true);
const goOffline = () => setIsOnline(false);
window.addEventListener('online', goOnline);
window.addEventListener('offline', goOffline);
return () => {
window.removeEventListener('online', goOnline);
window.removeEventListener('offline', goOffline);
};
}, []);
return isOnline;
}
function StatusBar() {
const isOnline = useOnlineStatus();
return <p>{isOnline ? 'Online' : 'Offline'}</p>;
}
function SaveButton() {
const isOnline = useOnlineStatus();
return <button disabled={!isOnline}>{isOnline ? 'Save' : 'Reconnecting…'}</button>;
}Browser ရဲ့ external value တစ်ခုကို subscribe လုပ်တဲ့ hook တွေအတွက် React မှာ useSyncExternalStore ဆိုတဲ့ built-in hook လည်း ရှိပါတယ်။ ဒီ lesson ရဲ့ useState + useEffect ပုံစံကို နားလည်ပြီးရင် အဲဒါကိုလည်း React docs မှာ ဆက်လေ့လာကြည့်ပါ။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေထဲက custom hook များ
အရွယ်အစား အလယ်အလတ် React project တစ်ခုရဲ့ src/hooks folder ကို ဖွင့်ကြည့်ရင် အောက်ကလို hook တွေ တွေ့ရလေ့ရှိပါတယ်။ အားလုံးရဲ့ ပုံစံက အတူတူပါ: browser ဒါမှမဟုတ် network နဲ့ ဆက်သွယ်တဲ့ ရှုပ်ထွေးမှုကို ဝှက်ထားပြီး component ကို ရိုးရှင်းတဲ့ value တစ်ခုပဲ ပေးပါတယ်။
- useDebouncedValue: search box မှာ စာရိုက်တိုင်း request မပို့ဘဲ ခဏ ရပ်သွားမှ value ကို update လုပ်ခြင်း။
- useLocalStorage: state ကို localStorage နဲ့ ချိတ်ထားလို့ page refresh လုပ်လည်း theme, draft စတာတွေ မပျောက်ခြင်း။
- useMediaQuery: CSS media query တစ်ခု ကိုက်မကိုက် boolean နဲ့ ပြန်ပေးလို့ mobile နဲ့ desktop layout ကို ခွဲရေးနိုင်ခြင်း။
- useFetch ဒါမှမဟုတ် useUser: data, error, loading state သုံးခုကို တစ်နေရာတည်းမှာ စီမံခြင်း၊ project ကြီးတွေမှာတော့ TanStack Query လို library ရဲ့ hook တွေကို သုံးလေ့ရှိပါတယ်။
Hook တွေက ရိုးရိုး function ဖြစ်လို့ test ရေးရတာလည်း လွယ်ပါတယ်။ Testing Library ရဲ့ renderHook လို tool တွေက hook တစ်ခုတည်းကို UI မပါဘဲ စမ်းသပ်ခွင့်ပေးပါတယ်။
အမှားများ
Custom hook နဲ့ ဖြစ်လေ့ရှိတဲ့ အမှားများ
Hook ကို condition ထဲမှာ ခေါ်ခြင်း
if (showWidth) { const width = useWindowWidth(); } လို code မှာ showWidth ပြောင်းသွားတဲ့ render မှာ hook အရေအတွက် ပြောင်းသွားပြီး React က 'Rendered more hooks than during the previous render' လို error ပေးပါတယ်။ Hook ကို အမြဲ top level မှာ ခေါ်ပြီး condition ကို ပြန်ရလာတဲ့ value ပေါ်မှာ စစ်ပါ။
Hook က component တွေကြား state ကို မျှပေးမယ်လို့ ထင်ခြင်း
useCart() ကို Header နဲ့ ProductPage နှစ်ခုလုံးမှာ ခေါ်ပြီး ProductPage မှာ item ထည့်ရင် Header ရဲ့ count က မပြောင်းပါဘူး — cart state နှစ်ခု သီးခြားစီ ဖြစ်နေလို့ပါ။ Value တစ်ခုတည်းကို မျှချင်ရင် state ကို context provider ထဲ ထားပြီး hook က useContext နဲ့ ဖတ်ခိုင်းပါ။
Hook argument ကို dependency ထဲ မထည့်ခြင်း
useFetch(url) ထဲက effect မှာ [] ပဲ ရေးထားရင် ပထမ url ကိုပဲ fetch လုပ်ပြီး နောက် url ပြောင်းသွားလည်း data အဟောင်း ပြနေပါတယ်။ Effect ထဲမှာ သုံးတဲ့ argument တိုင်း ([url]) ကို dependency array ထဲ ထည့်ပါ။
Mini challenge
Mini challenge: useDebouncedValue
Search page မှာ စာရိုက်တိုင်း search မလုပ်ချင်ပါဘူး။ Value တစ်ခုနဲ့ delay တစ်ခုကို ယူပြီး user ရိုက်တာ delay ကြာအောင် ရပ်သွားမှ value ကို ပြန်ပေးတဲ့ useDebouncedValue(value, delayMs) hook ကို ရေးပြီး SearchPage မှာ သုံးပါ။
- Hook က debounced value ကို state ထဲ ထားပြီး initial value ကို ပေးလိုက်တဲ့ value နဲ့ စရပါမယ်
- Effect က setTimeout နဲ့ update ကို နောက်ဆုတ်ပြီး cleanup မှာ clearTimeout လုပ်ရပါမယ်
- Dependency array မှာ value နဲ့ delayMs နှစ်ခုလုံး ပါရပါမယ်
- SearchPage က 'Typing: ...' နဲ့ 'Searching for: ...' ကို ခွဲပြရပါမယ်
import { useEffect, useState } from 'react';
function useDebouncedValue(value, delayMs) {
// TODO: state for the debounced value
// TODO: effect that waits delayMs, then copies value into that state
// TODO: cleanup that cancels the pending timer
// TODO: return the debounced value
}
export default function SearchPage() {
// TODO: query state for the input, then call useDebouncedValue(query, 300)
return null; // TODO: input + "Typing:" line + "Searching for:" line
}ရလဒ်: 'rea' လို့ မြန်မြန် ရိုက်ရင် Typing: rea ချက်ချင်း ပေါ်ပေမဲ့ Searching for: က အလွတ်အတိုင်း ကျန်ပါတယ်။ ရိုက်တာ 300ms ရပ်သွားမှ Searching for: rea ဖြစ်လာပြီး timer တစ်ခုတည်းပဲ ပြီးဆုံးပါတယ်။
အရိပ်အမြွက် ကြည့်ရန်
Cleanup က အဓိကပါ: စာလုံးအသစ် ရိုက်တိုင်း value ပြောင်းပြီး React က effect အဟောင်းရဲ့ cleanup ကို အရင် run ပါတယ်၊ ဒါကြောင့် timer အဟောင်း ပယ်ဖျက်ခံရပြီး နောက်ဆုံး timer တစ်ခုပဲ ကျန်ပါတယ်။
အဖြေ ကြည့်ရန်
import { useEffect, useState } from 'react';
function useDebouncedValue(value, delayMs) {
const [debounced, setDebounced] = useState(value);
useEffect(() => {
const id = setTimeout(() => setDebounced(value), delayMs);
return () => clearTimeout(id); // a newer value cancels the old timer
}, [value, delayMs]);
return debounced;
}
export default function SearchPage() {
const [query, setQuery] = useState('');
const debouncedQuery = useDebouncedValue(query, 300);
return (
<>
<input
aria-label="Search"
value={query}
onChange={(e) => setQuery(e.target.value)}
/>
<p>Typing: {query}</p>
<p>Searching for: {debouncedQuery}</p>
</>
);
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Custom hook ဆိုတာ use နဲ့ စပြီး တခြား hook တွေကို ခေါ်တဲ့ ရိုးရိုး function ပါ
- Hook က logic ကို မျှပေးပြီး ခေါ်တဲ့ component တိုင်းက state နဲ့ effect ကိုယ်ပိုင် ရပါတယ်
- Hook ကို component ဒါမှမဟုတ် hook ရဲ့ top level မှာပဲ ခေါ်ပါ၊ if, loop, early return နောက်မှာ မခေါ်ပါနဲ့
- Hook ထဲက effect မှာ argument တွေကို dependency အဖြစ် ထည့်ပြီး cleanup ကို မမေ့ပါနဲ့
React Documentation — React