티스토리 뷰

input값을 넘긴 후 input값을 초기화하려고 하는데 오류가 남!

class addForm extends Component {
  inputRef = createRef();

  addHabit = (e) => {
    e.preventDefault();
    const name = this.inputRef.current.value;
    this.props.addHabit(name);
    this.inputRef.current.reset();
  };
  ......

this.inputRef.current.reset is not a function

this.inputRef.current.reset는 함수가 아니다

왜..? reset()은 있는 건데... 왜...?


input에는 reset()을 사용할 수 없다

input을 감싸고 있는 form에 reset()을 해줘야함

class addForm extends Component {
  inputRef = createRef();
  formRef = createRef();

  addHabit = (e) => {
    e.preventDefault();
    const name = this.inputRef.current.value;
    this.props.addHabit(name);
    this.formRef.current.reset();
  };

 

참고

https://stackoverflow.com/questions/62412963/how-to-reset-input-field-from-useref-in-react

 

How to reset input field from useRef in React?

I have a text input. If I click on a specific button in the page, I want to reset the value of the input. Here is my code: const inputRef = useRef() const handleClick= () => { inputRef.

stackoverflow.com

 

728x90
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
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
글 보관함