const refContainer = useRef(초기값);
const App = () => {
const input = React.useRef(null);
const handleClick = () => {
if (input.current) {
}
}
return (
<div>
<input type="text" ref={input} />
<button type="button" onClick={handleClick}>Click to Reset</button>
</div>
);
}
const App = () => {
const input = React.useRef(null);
const handleClick = () => {
if (input.current) {
input.current.value = '';
}
}
const App = () => {
const input = React.useRef(null);
const handleClick = () => {
if (input.current) {
input.current.value = '';
}
}
return (
<div>
<input type="text" ref={input} />
<button type="button" onClick={handleClick}>Click to Reset</button>
</div>
);
}
const App = () => {
const input = React.useRef(null);
const [value, setValue] = React.useState('')
const handleClick = () => {
setValue('');
if (input.current) {
input.current.value = '';
}
}
return (
<div>
현재 value는 {value}입니다.
<input type="text" ref={input} onChange={(e) => setValue(e.target.value)} />
<button type="button" onClick={handleClick}>Click to Reset</button>
</div>
);
}
// setValue('');
onCange 이벤트가 발생하지 않고, value가 업데이트 되지 않기 때문에 렌더가 일어나지 않음
= 버튼을 눌렀을때 이전 text 값이 남아있음
// if (input.current) {
// input.current.value = '';
// }
ref 실제 dom element에 접근해서 value를 초기화하는 문장을 사용하지 않기 때문에
= input에 이전 text 값이 남아있음
const App = () => {
const [value, setValue] = React.useState('');
const handleClick = () => {
setValue('');
}
return (
<div>
현재 value는 {value}입니다.
<input type="text" **value={value}** onChange={(e) => setValue(e.target.value)} />
<button type="button" onClick={handleClick}>Click to Reset</button>
</div>
);
}
const App = () => {
const [value, setValue] = React.useState('');
const handleClick = () => {
setValue('');
}
return (
<div>
<input type="text" value={value} onChange={(e) => setValue(e.target.value)} />
<button type="button" onClick={handleClick}>Click to Reset and Foucs!</button>
</div>
);
}
const App = () => {
const input = React.useRef(null);
const [value, setValue] = React.useState('');
const handleClick = () => {
setValue('');
if (input.current) {
input.current.focus();
}
}
return (
<div>
<input type="text" ref={input} value={value} onChange={(e) => setValue(e.target.value)} />
<button type="button" onClick={handleClick}>Click to Reset</button>
</div>
);
}