Clear an input field with Reactjs?

后端 未结 7 2048
囚心锁ツ
囚心锁ツ 2020-12-08 04:33

I am using a variable below.

var newInput = {
   title: this.inputTitle.value,
   entry: this.inputEntry.value    
};

This is used by my i

7条回答
  •  独厮守ぢ
    2020-12-08 04:58

    Let me assume that you have done the 'this' binding of 'sendThru' function.

    The below functions clears the input fields when the method is triggered.

    sendThru() {
        this.inputTitle.value = "";
        this.inputEntry.value = "";
    }
    

    Refs can be written as inline function expression:

    ref={el => this.inputTitle = el}
    

    where el refers to the component.

    When refs are written like above, React sees a different function object each time so on every update, ref will be called with null immediately before it's called with the component instance.

    Read more about it here.

提交回复
热议问题