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

CSS Houdini achieves dynamic wave effect

CSS Houdini is known as the most exciting innovat...

How to deploy SpringBoot project using Docker

The development of Docker technology provides a m...

Causes and solutions to the garbled character set problem in MySQL database

Preface Sometimes when we view database data, we ...

Solution to slow response of Tomcat server

1. Analytical thinking 1. Eliminate the machine&#...

Advantages and Problems of XHTML CSS Website Design

XHTML is the standard website design language cur...

Vue directives v-html and v-text

Table of contents 1. v-text text rendering instru...

Detailed explanation of MySQL batch SQL insert performance optimization

For some systems with large amounts of data, the ...

How to configure MySQL master-slave synchronization in Ubuntu 16.04

Preparation 1. The master and slave database vers...

How to delete garbled or special character files in Linux

Due to encoding reasons, garbled characters will ...

Writing a shell script in Ubuntu to start automatically at boot (recommended)

The purpose of writing scripts is to avoid having...

How to change MySQL character set utf8 to utf8mb4

For MySQL 5.5, if the character set is not set, t...

Binary installation of mysql 5.7.23 under CentOS7

The installation information on the Internet is u...

MySQL 5.7.17 installation and configuration method graphic tutorial (windows10)

MySQL 5.7.17 installation and configuration metho...

Win2008 Server Security Check Steps Guide (Daily Maintenance Instructions)

The document has been written for a while, but I ...