useRef()
1. 언제 사용할까?
2. 용도
2.1 상태를 기억
const refContainer = useRef(initialValue);
2.2 DOM 객체 참조
3. useState vs useRef
Last updated
const refContainer = useRef(initialValue);
Last updated
const HannaButton = ({ ...props }) => {
return <button {...props} type="button" />;
};
export default function App() {
const paraRef = React.useRef(null);
React.useEffect(() => {}, []);
// 버튼 클릭시 p 요소의 글자 색상이 변경 된다.
const handleClick = () => {
paraRef.current.style.color = "rgb(167 25 25)"
};
return (
<div>
<h1>Hello StackBlitz!</h1>
<p ref={paraRef}>Start editing to see some magic happen :)</p>
<HannaButton onClick={handleClick}>change color</HannaButton>
</div>
);
}