React example of how to get the value of the input box

React example of how to get the value of the input box

React multiple ways to get the value of the input box

  • The first method is uncontrolled component acquisition
  • The second method is to obtain the controlled component

Uncontrolled component gets ref

import 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:
  • Solve the problem of react-native soft keyboard popping up and blocking the input box
  • How to solve the problem of input box being blocked by mobile keyboard in react android
  • React-Native makes a text input box component implementation code
  • react+antd.3x implements ip input box

<<:  Detailed explanation of how to solve the problem that the docker container cannot access the host machine through IP

>>:  MySQL 5.7.18 installation and configuration tutorial under Windows

Recommend

How to forget the password of Jenkins in Linux

1.Jenkins installation steps: https://www.jb51.ne...

About VSCode formatting JS automatically adding or removing semicolons

introduction It is okay to add or not add a semic...

Implementation of nacos1.3.0 built with docker

1. Resume nacos database Database name nacos_conf...

MySQL Error 1290 (HY000) Solution

I struggled with a problem for a long time and re...

In-depth explanation of nginx location priority

location expression type ~ indicates to perform a...

Simple example of adding and removing HTML nodes

<br />Simple example of adding and removing ...

How to install Nginx in CentOS

Official documentation: https://nginx.org/en/linu...

How to view and configure password expiration on Linux

With the right settings, you can force Linux user...

Why MySQL database avoids NULL as much as possible

Many tables in MySQL contain columns that can be ...

Let’s talk in detail about how browsers view closures

Table of contents Preface Introduction to Closure...