Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react class component input text with clear button

class App extends React.Component {
  constructor(props) {
    super(props);
    
    this.state = {
      data: ''
    }
    this.updateState = this.updateState.bind(this);
    this.clearData = this.clearData.bind(this);
    };
  updateState(e) {
    this.setState({data: e.target.value});
  }
  clearData() {
    this.setState({data: ''});
    ReactDOM.findDOMNode(this.refs.myInput).focus();
  }
  render() {
    return (
    <>
        <input type="text" value = {this.state.data} onChange = {this.updateState} ref = "myInput"/>
        <button onClick={this.clearData}>CLEAR</button>
        <h4>{this.state.data}</h4>
        </>
             );
  }
}
        ReactDOM.render(
        <App />, document.getElementById('mountNode') );
Comment

react clear input after button click

<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>
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to check if date is between two dates in javascript 
Javascript :: Implementing state lifecycle in react class component 
Javascript :: adding all elements in an array javascript 
Javascript :: vuejs slots events 
Javascript :: what is ... in javascript 
Javascript :: custom search filter in angular 8 
Javascript :: vscode jest disable auto run 
Javascript :: loadstring json flutter 
Javascript :: Load JSON from file robotframework 
Javascript :: how to use moment to compare time for calendar change color 
Javascript :: addeventlistener javascript multiple functions 
Javascript :: leafletjs openstreets example 
Javascript :: creating room in ws nodejs 
Javascript :: vue global computed property 
Javascript :: ajax 
Javascript :: what is regular expression in javascript 
Javascript :: JSON requests using API in Javascript 
Javascript :: slice 
Javascript :: mongoose search query for a word in a field 
Javascript :: javascript loop backwards 
Javascript :: get contents between tags javascript 
Javascript :: require("readline") noe js 
Javascript :: javascript access pushed element 
Javascript :: js compare tow object values 
Javascript :: jquery on method 
Javascript :: what is prototype javascript 
Javascript :: splice 
Javascript :: js regex find text inside single quotes 
Javascript :: how to use yarn to create next app 
Javascript :: js add fields to map 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =