useRef는 .current 프로퍼티로 전달된 인자(initialValue)로 초기화된 변경 가능한 ref 객체를 반환합니다.
특정 Dom을 선택할 때
ex) 초기화 시 focus 처리
컴포넌트 안에서 조회 및 수정 가능한 변수를 생성할 때(랜더링되지 않으면서)
ex) onChange 없이 input 관리하기
- State / Ref 변경 시 리렌더링 확인하기
import React, { useRef, useState } from 'react';
import siroo from './siroo.jpg'
const RefSample1 = () => {
const a = 1;
const [state, setState] = useState(1);
const ref = useRef(1);
return (
<div>
<h1>1. 변경은 관리O! 리렌더링을 발생X!</h1>
<ul>
<li>일반 변수: {a}</li>
<li>state 변수: {state}</li>
<li>ref 변수: {ref.current}</li>
</ul>
<img src={siroo} width="300" height="300" alt=""/>
<br/>
<button onClick={() => setState(state + 1)}>hello</button>
<button onClick={() => (ref.current = ref.current + 1)}>
집사야 눌러봐
</button>
</div>
);
};
export default RefSample1;
- Ref로 input값 관리하기
import React, { useRef, useState } from "react";
const SignInForm = (props) => {
const { signInList, setSignInList, articleNumber } = props;
// *************************1. Ref 객체 만들기 **********************
const inputName = useRef(null);
const inputAge = useRef(null);
const inputEmail = useRef(null);
const inputPassword = useRef(null);
// ************************ 3.current로 돔 접근하기 *****************
const signUpByRef = () => {
const newSignInForm = {
articleNumber : articleNumber.current,
name: inputName.current.value,
age: inputAge.current.value,
email: inputEmail.current.value,
password: inputPassword.current.value,
};
setSignInList([...signInList, newSignInForm]);
alert("회원가입되셨습니다.");
inputName.current.value = "";
inputAge.current.value = "";
inputEmail.current.value = "";
inputPassword.current.value = "";
articleNumber.current += 1;
};
const reset = () => {
inputName.current.value = "";
inputAge.current.value = "";
inputEmail.current.value = "";
inputPassword.current.value = "";
inputName.current.focus();
};
return (
<div>
<h1>회원가입 폼 By REF</h1>
<div>
<ul>
<li>
<label>아이디</label>
<input
name="name"
type="text"
// ********************2. 선택할 DOM에 ref 값 설정****************************
ref={inputName}
//***************************************************************************
></input>
</li>
<li>
<label>나이</label>
<input
name="age"
type="text"
ref={inputAge}
></input>
</li>
<li>
<label>이메일</label>
<input
name="email"
type="text"
ref={inputEmail}
></input>
</li>
<li>
<label>비밀번호</label>
<input
name="password"
type="password"
ref={inputPassword}
></input>
</li>
</ul>
</div>
<div>
<button onClick={reset}> 초기화 </button>
<button onClick={signUpByRef}> 회원가입 </button>
</div>
</div>
);
};
export default SignInForm;
- ref로 화면 리사이징하기
import { observer } from 'mobx-react';
import React, { useEffect, useRef, useState } from 'react';
import KitchenOrderFrame from '../../component/kitchen/KitchenOrderFrame';
import UseStore from '../../stores/UseStore';
import EventSource from 'eventsource';
import ArrowIcon from '../../component/kitchen/ArrowIcon';
import EmptyBox from '../../component/kitchen/EmptyBox';
import { NOTI_KITCHEN } from '../../utils/config';
import './index.css';
const Kitchen = observer(() => {
const { OrderStore } = UseStore();
const [topNum, setTopNum] = useState(0);
const kitchenContainer = useRef();
//메인 useEffect : 초기 목록조회, 사이징처리, sse세팅
useEffect(() => {
OrderStore.fetchOrdersInProgress().then();
resizing();
const es_ = new EventSource(NOTI_KITCHEN);
es_.onmessage = (e) => {
const reqWaitingNum = JSON.parse(e.data).data.waitingNum;
OrderStore.addOrdersInProgressBySSE(reqWaitingNum);
};
return () => {
es_.close();
};
}, [OrderStore]);
//서브 useEffect : 리사이징 함수 호출
useEffect(() => {
window.addEventListener('resize', resizing);
return () => {
window.removeEventListener('resize', resizing);
};
}, []);
// window 크기가 변할때마다 노출 갯수 변경
const resizing = () => {
setTopNum(() =>
Math.floor((kitchenContainer.current.clientWidth -140) / 326)
);
};
return (
<>
<div className={'kitchen-container'} ref={kitchenContainer}>
<div className={'left-arrow-space'}>
<ArrowIcon direction={'left'} />
</div>
{OrderStore.ordersInProgressOnView.length === 0 ? (
<EmptyBox />
) : (
OrderStore.ordersInProgressOnView
.slice(undefined, topNum)
.map((order) => (
<KitchenOrderFrame key={order.orderId} order={order} />
))
)}
<div className={'right-arrow-space'}>
<ArrowIcon direction={'right'} />
</div>
</div>
</>
);
});
export default Kitchen;