Hookμ κ·μΉ
- React ν¨μ κ΅¬μ± μμ λ΄μμλ§ νΈμΆν μ μμ(λ°λμ Functional Component μμμ μ¬μ©ν΄μΌ ν¨)
- κ΅¬μ± μμμ μ΅μμμμλ§ νΈμΆν μ μμ
- 쑰건문, λ°λ³΅λ¬Έ μμμ λͺ» μ
Custom Hook
- λ κ°μ μλ°μ€ν¬λ¦½νΈ ν¨μμμ κ°μ λ‘μ§μ 곡μ νκ³ μ ν λλ λ λ€λ₯Έ ν¨μλ‘ λΆλ¦¬ ν¨.
- μ€λ³΅λ μ½λλ₯Ό λ°κ²¬νλ©΄ μ¬μ¬μ©μ΄ κ°λ₯ν ν¨μλ‘ λ§λ€μ΄μ μ€λ³΅μ μ κ±°ν μ μμ.
- 컀μ€ν ν λ΄λΆμμ λ€λ₯Έ ν λ€μ μμ λ‘κ² μ‘°ν©κ°λ₯(νμν μΈν°νμ΄μ€μ κΈ°λ₯ μΆκ° κ°λ₯)
- 컀μ€ν ν λ΄λΆμμ 컀μ€ν ν μ΄ κ°μ§λ μ€ν μ΄νΈμ μ΄ννΈλ μμ ν λ 립μ μ΄κΈ° λλ¬Έμ μ¬μ¬μ©μ΄ μμ£Ό μ’μ
use0000 μ²λΌ λ°λμ 맨 μμ useλΌλ λ¨μ΄λ₯Ό λΆμ¬μ λ€λ₯Έ κ°λ°μλ€μ΄ Hookμ΄λΌλ κ±Έ μ μ μκ² ν΄μ€μΌ ν¨.
컀μ€ν ν μμ
1. useAsync
λΉλκΈ° ν¨μμ λ‘λ©, μλ¬ μ²λ¦¬λ₯Ό νλ λ° μ¬μ©ν μ μλ ν¨μ
function useAsync(asyncFunction) {
const [pending, setPending] = useState(false);
const [error, setError] = useState(null);
const wrappedFunction = useCallback(async (...args) => {
setPending(true);
setError(null);
try {
return await asyncFunction(...args);
} catch (error) {
setError(error);
} finally {
setPending(false);
}
}, [asyncFunction]);
return [pending, error, wrappedFunction];
}
2.useToggle
toggle ν¨μλ₯Ό νΈμΆν λλ§λ€ value κ°μ΄ μ°Έ/κ±°μ§μΌλ‘ λ²κ°μκ°λ©° λ°λ (ON/OFF μ€μμΉ λ±μ μ¬μ©)
function useToggle(initialValue = false) {
const [value, setValue] = useState(initialValue);
const toggle = () => setValue((prevValue) => !prevValue);
return [value, toggle];
}
μ°Έκ³ μλ£ λ° μΆμ²
https://github.com/streamich/react-use
GitHub - streamich/react-use: React Hooks — π
React Hooks — π. Contribute to streamich/react-use development by creating an account on GitHub.
github.com
https://ko.reactjs.org/docs/hooks-custom.html
μμ λ§μ Hook λ§λ€κΈ° – React
A JavaScript library for building user interfaces
ko.reactjs.org
https://www.w3schools.com/react/react_customhooks.asp
React Custom Hooks
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
www.w3schools.com
μ½λμ - Reactλ‘ λ°μ΄ν° λ€λ£¨κΈ° κ°μ
'FA > React' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
20221104 React Hooks - useReducer (0) | 2022.11.04 |
---|---|
20221103 React - κ°μλ (Virtual DOM) (0) | 2022.11.03 |
20221102 React Hooks - useMemo, useCallback (0) | 2022.11.02 |
20221024 input type="file" μ©λ μ ννκΈ° (0) | 2022.10.24 |
20221008 video μ¬μ©νκΈ° (0) | 2022.10.08 |