전달된 인수
로 초기화 된 변경 가능한 ref 객체를 반환바뀌지 않는다
.공통적으로 current속성을 가진 객체를 반환한다.
import React, { useRef } from "react";
const Focus = () => {
const firstRef = useRef(null);
const secondRef = useRef(null);
const thirdRef = useRef(null);
const handleInput = (event) => {
console.log(event.key, event);
if (event.key === "Enter") {
console.log(event.target)
console.log(firstRef)
if (event.target === firstRef.current) {
secondRef.current.focus();
event.target.value = "";
} else if (event.target === secondRef.current) {
thirdRef.current.focus();
event.target.value = "";
} else if (event.target === thirdRef.current) {
firstRef.current.focus();
event.target.value = "";
} else {
return;
}
}
};
return (
<div>
<div>
<input ref={firstRef} onKeyUp={handleInput} />
</div>
<div>
<input ref={secondRef} onKeyUp={handleInput} />
</div>
<div>
<input ref={thirdRef} onKeyUp={handleInput} />
</div>
</div>
);
};
export default Focus;