Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- background-color 속성
- i 태그
- RGBA
- padding 속성
- go live
- Checked 의사 클래스
- RGB
- 아두이노
- width속성
- iframe
- tag html
- focus 의사클래스
- 전체 선택자
- 일반 형제 결합자
- Live Server
- br 태그
- 임베디드
- sup태그
- id 선택자
- 자식결합자
- iframe 태그
- sub태그
- html
- not 의사클래스
- 인접 형제 결합자
- height속성
- html 태그
- css
- html tag i
- reveal in file explorer
Archives
- Today
- Total
so woon!
[REACT] React Hook Form - setValue 사용하기 본문
학습일 : 2023. 04. 18
submit 이 정상적으로 되면 input 에 값을 비워주도록 할텐데
setValue를 사용해보자
<ToDoList.tsx>
useForm 작성해준 부분에 setValue도 추가시킨다음
onSubmit 함수에 setValue를 적어주면
정상적으로 submit이 된다면 string이 비워지게 될 것이다.
import { useForm } from "react-hook-form";
interface IForm {
toDo: string;
}
function ToDoList() {
const { register, handleSubmit, setValue } = useForm<IForm>();
const onSubmit = (data: IForm) => {
console.log("add to do", data.toDo);
setValue("toDo", ""); // submit 되면 문자열이 비워짐
};
return (
<div>
{/* handleSubmit 함수를 사용할 때는
첫번째 매개변수로 데이터가 유효할 때 호출되는 다른 함수를 받는다.
두번째 매개변수로는 데이터가 유효하지 않을때 호출될 함수를 넣으면 된다.*/}
<form onSubmit={handleSubmit(onSubmit)}>
<input
{...register("toDo", {
required: "please write a to do",
})}
placeholder="오늘 해야할 일을 입력하세요"
/>
<button>추가</button>
</form>
</div>
);
}
export default ToDoList;
실행결과
콘솔에는 적어준게 잘 찍혀나오고
제대로 submit이 되었기 때문에
적어준 것들이 비워짐
'ReactJS > 개념정리' 카테고리의 다른 글
[REACT] todolist 만들기 (0) | 2023.04.18 |
---|---|
[REACT] useRecoilState 사용하기 (0) | 2023.04.18 |
[REACT] React Hook Form - validate 옵션 사용하기 (0) | 2023.04.17 |
[REACT] React Hook Form - setError (새로운 에러를 만드는 법) (1) | 2023.04.17 |
[REACT] React Hook Form - setError 사용하기 (0) | 2023.04.17 |
Comments