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 :: obtain only integer not decimal js 
Javascript :: number vs bigint js 
Javascript :: difference 
Javascript :: readystate==4 
Javascript :: how to loop through all tags in html 
Javascript :: javascript this Inside Constructor Function 
Javascript :: Using the Sanity client without specifying an API version is deprecated 
Javascript :: launch json for golang with args 
Javascript :: outputstream to image js example 
Javascript :: write buffer to file in node 
Javascript :: angular autofocus 
Javascript :: socket io express 
Javascript :: vue back image 
Javascript :: how to generate random gradient javascript 
Javascript :: javascript decrement 
Javascript :: remove array value by index js 
Javascript :: service worker self.clients 
Javascript :: Match All Letters and Numbers freecodecamp 
Javascript :: slice array jas 
Javascript :: alex morgan 
Javascript :: random between min and max 
Javascript :: automated counter with react hooks 
Javascript :: selected option using javascript 
Javascript :: creating room in ws node js 
Javascript :: Find the Longest Word in a String 
Javascript :: gsap keyframes 
Javascript :: express and node pakages 
Javascript :: convert matrix string to matrix javascript 
Javascript :: hide urls in .env in react app 
Javascript :: array map sort descendeing 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =