Clear an input field with Reactjs?

后端 未结 7 2004
囚心锁ツ
囚心锁ツ 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:36

    Declare value attribute for input tag (i.e value= {this.state.name}) and if you want to clear this input vale you have to use this.setState({name : ''})

    PFB working code for your reference :

    <script type="text/babel">
        var StateComponent = React.createClass({
            resetName : function(event){
                this.setState({
                    name : ''
                });
            },
            render : function(){
                return (
                    <div>
                        <input type="text" value= {this.state.name}/>
                        <button onClick={this.resetName}>Reset</button>
                    </div>
                )
            }
        });
        ReactDOM.render(<StateComponent/>, document.getElementById('app'));
    </script>
    
    0 讨论(0)
  • 2020-12-08 04:48

    You can use input type="reset"

    <form action="/action_page.php">
      text: <input type="text" name="email" /><br />  
      <input type="reset" defaultValue="Reset" />  
    </form>
    
    0 讨论(0)
  • 2020-12-08 04:57

    I have a similar solution to @Satheesh using React hooks:

    State initialization:

    const [enteredText, setEnteredText] = useState(''); 
    

    Input tag:

    <input type="text" value={enteredText}  (event handler, classNames, etc.) />
    

    Inside the event handler function, after updating the object with data from input form, call:

    setEnteredText('');
    

    Note: This is described as 'two-way binding'

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-08 04:58

    I'm not really sure of the syntax {el => this.inputEntry = el}, but when clearing an input field you assign a ref like you mentioned.

    <input type="text" ref="someName" />
    

    Then in the onClick function after you've finished using the input value, just use...

    this.refs.someName.value = '';
    

    Edit

    Actually the {el => this.inputEntry = el} is the same as this I believe. Maybe someone can correct me. The value for el must be getting passed in from somewhere, to act as the reference.

    function (el) {
        this.inputEntry = el;
    }
    
    0 讨论(0)
  • 2020-12-08 05:00

    On the event of onClick

    this.state={
      title:''
    }
    
    sendthru=()=>{
      document.getElementByid('inputname').value = '';
      this.setState({
         title:''
    })
    }
    
    <input type="text" id="inputname" className="form-control" ref={el => this.inputTitle = el} />   
    <button className="btn btn-info" onClick={this.sendthru}>Add</button>
    
    0 讨论(0)
提交回复
热议问题