const ref = useRef(value);
값은 {current:value}
즉, useRef는 변화는 감지해야 하지만 렌더링될때마다 변경이 되지는 않아야 할 때 사용한다
const countRef = useRef(0);
<p>Ref : {countRef.current}</p>
useRef 변수인 countRef의 값을 가져오려면 {countRef.current}
state변화 → 렌더링 → 컴포넌트 내부 변수들 초기화
그래서 원하지 않는 렌더링때문에 곤란해질때가 있다.
ref의 변화 → 렌더링X → 변수들의 값이 유지됨
state변화 → 렌더링 → 그래도 ref의 값은 유지됨
const increaseCountState = () => {
setCount(count + 1);
};
const increaseCountRef = () => {
countRef.current++;
};
값이 엄청 자주 바뀌는 값을 state에 넣어놓게 되면 계속 렌더링되므로 성능에 매우 안좋아진다.
근데 이 때 ref를 사용하면 렌더링되지 않으므로 성능이 더 좋아진다!!
변수는 렌더링이 되면 계속해서 0으로 초기화가 되지만 ref는 렌더링이 되어도 초기화가 되지 않는다!
ref={ref변수명}
손쉽게 input요소에 접근
할 수 있음const ref = useRef(value);
<input ref={ref}/>
⇒ 이렇게 하면 바로 ref 변수에 접근할 수 있다!
그저 ref={ref변수명}
을 쓰면 된다
const inputRef = useRef();
useEffect(() => {
inputRef.current.focus();
}, []);
const login = () => {
alert(`환영합니다. ${inputRef.current.value}`);
inputRef.current.focus();
};
<input ref={inputRef} type="text" placeholder="username" />
<button onClick={login}>login</button>
import { useState, useRef, useEffect } from "react";
import "./App.css";
function App() {
const [count, setCount] = useState(1);
const renderCount = useRef(1);
useEffect(() => {
renderCount.current++;
console.log("렌더링수:", renderCount.current);
});
const countRef = useRef(0);
let countVar = 0;
const [renderer, setRenderer] = useState(0);
const countRef2 = useRef(0);
console.log(countRef);
const increaseCountState = () => {
setCount(count + 1);
};
const increaseCountRef = () => {
countRef.current++;
console.log("ref:", countRef.current);
};
const increaseRef = () => {
countRef2.current++;
console.log(`ref: ${countRef2.current}`);
};
const increaseVar = () => {
countVar++;
console.log(`var : ${countVar}`);
};
const doRender = () => {
setRenderer(renderer + 1);
};
const printResults = () => {
console.log(`ref: ${countRef.current}, var: ${countVar}`);
};
const inputRef = useRef();
useEffect(() => {
inputRef.current.focus();
}, []);
const login = () => {
alert(`환영합니다. ${inputRef.current.value}`);
inputRef.current.focus();
};
return (
<>
<p>State: {count} </p>
<p>Ref : {countRef.current}</p>
<button onClick={increaseCountState}>State 올려</button>
<button onClick={increaseCountRef}>Ref 올려</button>
<p>Ref : {countRef.current}</p>
<p>Var : {countVar}</p>
<button onClick={doRender}>render</button>
<button onClick={printResults}>Ref, Var 값 출력</button>
<button onClick={increaseRef}>Ref 올려</button>
<button onClick={increaseVar}>Var 올려</button>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>올려</button>
<div>
<input ref={inputRef} type="text" placeholder="username" />
<button onClick={login}>login</button>
</div>
</>
);
}
export default App;