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

JavaScript microtasks and macrotasks explained

Preface: js is a single-threaded language, so it ...

Implementing Binary Search Tree in JavaScript

The search binary tree implementation in JavaScri...

Full-screen drag upload component based on Vue3

This article mainly introduces the full-screen dr...

Graphic tutorial on installing CentOS7 on VMware 15.5

1. Create a new virtual machine in VMware 15.5 1....

JavaScript to implement input box content prompt and hidden function

Sometimes the input box is small, and you want to...

Detailed installation history of Ubuntu 20.04 LTS

This article records the creation of a USB boot d...

The webpage cannot be opened because the div element lacks a closing tag

At first I thought it was a speed issue, so I late...

Detailed explanation of MySQL phantom reads and how to eliminate them

Table of contents Transaction Isolation Level Wha...

Why do select @@session.tx_read_only appear in DB in large quantities?

Find the problem When retrieving the top SQL stat...

Write a publish-subscribe model with JS

Table of contents 1. Scene introduction 2 Code Op...

Use of environment variables in Docker and solutions to common problems

Preface Docker can configure environment variable...

CSS uses the autoflow attribute to achieve seat selection effect

1. Autoflow attribute, if the length and width of...