๐React - ืืืจืื
ืืืืื ืฉืื ืืืจ ืฉืื ืขื ื ืืืื ืฆื ืืคืจืงืื ื ืคืจืืื.

Custom Hooks โ ืืืจืื ืขื ืืืืืืืช ืืืืช
ืื ืื Custom Hook ืืืื ืืืฉืชืืฉ?
Custom Hook ืืื ืคืื ืงืฆืื ืฉืืชืืืื ื-use ืืืืคืฉืจืช ืืืื ืืืืืงืช Hooks ืืฉืืืืฉ ืืืืจ ืืื ืงืืืคืื ื ืืืช. ืืืืืื: ื ืจืืืช/ืืกืชืจื (toggle), ืฉืืืคืช ื ืชืื ืื (fetch), ืืืืืื ืก/ืชืืืื, ื ืืืื ืืืืื ืื ืืขืื โ ืชืื ืฉืืืจื ืขื ืืืงื Hooks ืืขื ืงืื ื ืงื ืืงืจืื.
ืืืงื ืืื
- ื-Hook ืืืื ืืืชืืื ืืฉื
use. - ืืืชืจ ืืงืจืื ื-Hooks ืจืง ืืจืื ืืขืืืื ื ืฉื ื-Hook (ืื ืืชืื ืชื ืื/ืืืืื).
- ืื ืชืืืช ืฉืืืคืืขื ืืืืืืงืช ื-Hook ืืืืืช ืืืืคืืข ืืืขืจืื ืืชืืืชืื.
- ืฉืืจื ืขื ืืืฉืง ืคืฉืื ืืืจืืจ: ืืืืืจื ืืืืืืงื/ืืืคื ืงืื ืขื ืื ืฉืืืืช ืฆืจืื.
- ืคืจืงื ืืืืืงืืช: ืขืืืฃ ืืื Hooks ืงืื ืื ืืืฉืจ ืืคืืฆืช ืืืช.
ืชืืื ืขื ืืื ืื
1) useToggle โ ืืชื ื ืจืืืช
Hook ืงืื ืื ืืืื ืืืืืื (ื ืจืืืช/ืืกืชืจื). ืฉืืืืฉื ืืืืืืื, ืืืืืื, ืืงืืจืืืื ืืื'.
import { useState } from 'react';
function useToggle(defaultValue = false) {
const [value, setValue] = useState(!!defaultValue);
const toggle = () => setValue(v => !v);
const on = () => setValue(true);
const off = () => setValue(false);
return { value, toggle, on, off };
}
export default useToggle;ืืืืื ืืื
toggle custom hook
some stuff
ืงืื ืืืืืื
import useToggle from './useToggle';
function ToggleExample() {
const { value: show, toggle } = useToggle(true);
return (
<div>
<h4>toggle custom hook</h4>
<button className='btn' onClick={toggle}>toggle</button>
{show && <h4>some stuff</h4>}
</div>
);
}
export default ToggleExample;2) useFetch โ ืฉืืืคืช ื ืชืื ืื ืขื Multiple Returns
Hook ืืืื ืฉืืืืืจ isLoading, isError, data. ืืืคื ื-AbortController ืื ืืงืืื, ืืื ืื ืืคื ืกืืจ Loading โ Error โ Success.
import { useState, useEffect } from 'react';
function useFetch(url, options = {}) {
const [isLoading, setIsLoading] = useState(true);
const [isError, setIsError] = useState(false);
const [data, setData] = useState(null);
useEffect(() => {
if (!url) { setIsError(true); setIsLoading(false); return; }
const ctrl = new AbortController();
(async () => {
try {
const res = await fetch(url, { signal: ctrl.signal, ...options });
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const json = await res.json();
setData(json);
} catch (err) {
if (err.name !== 'AbortError') setIsError(true);
} finally {
setIsLoading(false);
}
})();
return () => ctrl.abort();
}, [url]); // ืืืืื ื ืชืืื ืจืง ื-url, ืืคืฉืจ ืืืจืืื ื-options ืื ืืฆืื
return { isLoading, isError, data };
}
export default useFetch;ืืืืื ืืื โ ืคืจืืคืื GitHub (QuincyLarson)
โ Loadingโฆ
ืงืื ืืืืืื
import useFetch from './useFetch';
const url = 'https://api.github.com/users/QuincyLarson';
function FetchData() {
const { isLoading, isError, data: user } = useFetch(url);
if (isLoading) return <h2>Loading...</h2>;
if (isError) return <h2>There was an error...</h2>;
const { avatar_url, name, company, bio } = user || {};
return (
<div>
<img style={{ width: 150, borderRadius: 25 }} src={avatar_url} alt={name} />
<h2>{name}</h2>
{company && <h4>works at {company}</h4>}
<p>{bio}</p>
</div>
);
}
export default FetchData;headers ืขื ืืืงื, ืื ืืืืืืฃ ืืชืืืช API.3) useFetchPerson โ ืืจืืืฆืื ืืืืงืืช ืืืืืืืงื "ืืฉืชืืฉ"
ืืืชื ืืืืืงื, ืื ืขื state ืืืืงื ืืฉื user ืืืืืจืช ืืืืื ื ืืงืืืคืื ื ืื ืืงืืจืืช.
import { useState, useEffect } from 'react';
function useFetchPerson(url) {
const [isLoading, setIsLoading] = useState(true);
const [isError, setIsError] = useState(false);
const [user, setUser] = useState(null);
useEffect(() => {
if (!url) { setIsError(true); setIsLoading(false); return; }
const ctrl = new AbortController();
(async () => {
try {
const res = await fetch(url, { signal: ctrl.signal });
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const json = await res.json();
setUser(json);
} catch (err) {
if (err.name !== 'AbortError') setIsError(true);
} finally {
setIsLoading(false);
}
})();
return () => ctrl.abort();
}, [url]);
return { isLoading, isError, user };
}
export default useFetchPerson;ืืืืื ืืื โ ืืืชื ืืืืืจ ืขื Hook ืืืขืืื
โ Loadingโฆ
ืืืคืื ืืืืฉืื
- ืืืฆืืื ืื-Hook ืจืง ืื ืฉืืืืช ืฆืจืื โ ืฉืืจื ืขื API ืงืื.
- ืื ืชืืฉืคื setState ืืืืื ืื ืืื ืฆืืจื โ ืืืืืจื ืคืขืืืืช (ืืื
toggle/on/off). - ื-fetch: ืฆืจืคื ื ืืงืื ืขื
AbortController, ืืืคืื ื-!res.ok. - ืืชืืืื ืืช ืืขืจืื ืืชืืืชืื ืื ืฉื-Hook ืืืื ืฆืคืื ืืื ืืคืชืืข ืืจืืฆืืช ืืืืชืจืืช.
- ืฉืงืื ืคืืฆืื ื-Hooks ืงืื ืื ื ืคืจืืื ืืืงืื Hook ืขื ืง ืขื ืืจืื ืืฆืืื.