React multiple ways to get the value of the input box
Uncontrolled component gets refimport React , {Component} from 'react'; export default class App extends Component { search(){ const inpVal = this.input.value; console.log(inpVal); } render(){ return( <div> <input type="text" ref={input => this.input = input} defaultValue="Hello"/> <button onClick={this.search.bind(this)}></button> </div> ) } } Use defaultValue to represent the default state of a component. It will only be rendered once and subsequent rendering will not work. The value of the input does not change with external changes, but is changed by its own state. Controlled component this.setState({})import React , {Component} from 'react'; export default class App extends Component { constructor(props){ super(props); this.state = { inpValu:'' } } handelChange(e){ this.setState({ inpValu:e.target.value }) } render(){ return( <div> <input type="text" onChange={this.handelChange.bind(this)} defaultValue={this.state.inpValu}/> </div> ) } } The value of the input box will change as the user input changes. onChange obtains the changed state through object e and updates the state. setState triggers view rendering according to the new state to complete the update. This is the end of this article about how to use react to get the value of an input box. For more information about how to use react to get the value of an input box, please search for previous articles on 123WORDPRESS.COM or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
>>: MySQL 5.7.18 installation and configuration tutorial under Windows
1.Jenkins installation steps: https://www.jb51.ne...
If your computer is a Mac, using homebrew to inst...
introduction It is okay to add or not add a semic...
Linux virtual machine: VMware + Ubuntu 16.04.4 Wi...
1. Resume nacos database Database name nacos_conf...
I struggled with a problem for a long time and re...
Basic network configuration Although Docker can &...
location expression type ~ indicates to perform a...
<br />Simple example of adding and removing ...
As the most commonly used layout element, DIV play...
Official documentation: https://nginx.org/en/linu...
With the right settings, you can force Linux user...
Many tables in MySQL contain columns that can be ...
Downloaded an es image from docker hub, version 6...
Table of contents Preface Introduction to Closure...