1
const [state, setState] = useState(initialState);
state
μ initialState λ κ°μ΄ κ°λ€.setState()
λ‘ μνκ°(state
)μ λ³κ²½ν μ μλ€.1
useEffect(didUpdate);
1
2
3
4
5
6
7
8
9
// componentDidMount
useEffect(() => {
console.log('첫 λ λλ§!')
},[])
// componentDidUpdate
useEffect(() => {
console.log('state κ°μ΄ λ³κ²½λ λλ§λ€ μ€ν!')
},[state])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// μ
λ°μ΄νΈ & μΈλ§μ΄νΈ
useEffect(() => {
console.log('mount!')
// clearn up
return() => {
console.log('unmount!')
}
})
// μΈλ§μ΄νΈ μΌ κ²½μ°μλ§
useEffect(() => {
console.log('mount!')
// clearn up
return() => {
console.log('unmount!')
}
},[]) // λλ²μ§Έ μΈμμ λΉ λ°°μ΄κ° μΆκ°
1
const [state, dispatch] = useReducer(reducer, initialArg, init);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const initialState = {count: 0};
function reducer(state, action) {
switch (action.type) {
case 'increment':
return {count: state.count + 1};
case 'decrement':
return {count: state.count - 1};
default:
throw new Error();
}
}
// component
function Counter() {
const [state, dispatch] = useReducer(reducer, initialState);
return (
<>
Count: {state.count}
<button onClick={() => dispatch({type: 'decrement'})}>-</button>
<button onClick={() => dispatch({type: 'increment'})}>+</button>
</>
);
}
initialState
μ reducer()
λ₯Ό μΈλΆ νμΌλ‘ λΆλ¦¬ν΄μ μ¬μ©ν μλ μλ€.1
const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]);
1
2
3
4
5
6
const memoizedCallback = useCallback(
() => {
doSomething(a, b);
},
[a, b],
);
1
const refContainer = useRef(initialValue);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function TextInputWithFocusButton() {
const inputEl = useRef(null);
const onButtonClick = () => {
// current λ ref κ°μ΄ inputEl μΈ element λ₯Ό κ°λ¦¬ν¨λ€.
inputEl.current.focus();
};
return (
<>
<input ref={inputEl} type="text" />
<button onClick={onButtonClick}>Focus the input</button>
</>
);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import React, { useRef } from 'react';
const RefSample = () => {
const id = useRef(1);
const setId = (n) => {
id.current = n;
}
const printId = () => {
console.log(id.current);
}
return (
<div>refsample</div>
);
};
export default RefSample;
1
useImperativeHandle(ref, createHandle, [deps])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// children
import React, { useRef, forwardRef, useImperativeHandle } from "react";
function FancyInput(props, ref) {
const inputRef = useRef();
useImperativeHandle(ref, () => ({
focus: () => {
inputRef.current.focus();
}
}));
return <input ref={inputRef} />;
}
export default forwardRef(FancyInput);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// parent
import React, { useRef } from "react";
import FancyInput from "./fancyInput";
function ParentComp(props, ref) {
const FancyInputRef = useRef();
const handleClick = () => {
if (FancyInputRef.current) {
FancyInputRef.current.focus();
}
};
return (
<div>
<FancyInput ref={FancyInputRef} />
<button onClick={handleClick}>λ²νΌ</button>
</div>
);
}
export default ParentComp;
useEffect()
μ ννκ° κ°λ€.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
useLayoutEffect(() => {
effect
return () => {
cleanup
};
}, [input])
// ex
const [name, setName] = useState("");
const [age, setAge] = useState(0);
useLayoutEffect(() => {
setAge(25);
setName("sw");
}, []);
1
2
3
4
5
6
7
function useFriendStatus(friendID) {
const [isOnline, setIsOnline] = useState(null);
useDebugValue(isOnline ? 'Online' : 'Offline');
return isOnline;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// useInputs
import { useState, useCallback } from 'react';
function useInputs(initialForm) {
const [form, setForm] = useState(initialForm);
const onChange = useCallback(e => {
const { name, value } = e.target;
setForm(form => ({ ...form, [name]: value }));
}, []);
const reset = useCallback(() => setForm(initialForm), [initialForm]);
return [form, onChange, reset];
}
export default useInputs;
https://ko.reactjs.org/docs/hooks-intro.html
https://velog.io/@velopert/react-hooks