Table of Contents
Form
Controlled input
textarea element
select element
input file element
Home Web Front-end JS Tutorial Detailed explanation of using forms in React

Detailed explanation of using forms in React

May 24, 2018 pm 02:22 PM
react form Detailed explanation

This time I will bring you a detailed explanation of the use of forms in React. What are the precautions when using forms in React? Here are practical cases, let’s take a look.

Form

React is a one-way data flow framework, so the form elements are different from other DOM elements, and the operation is also very different from the two-way binding framework. So I’ll talk about it separately here.

Controlled input

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

import React from 'react'

import ReactDOM from 'react-dom'

class Component1 extends React.Component{

    constructor(props){

        super(props)

        this.state = {

            text: 'Hello React'

        }

    }

    render(){

        return (

            <p>

                <p><label>写死value-锁定状态</label><input type="text" value="hello react"/></p>

                <p><label>动态value-锁定状态</label><input type="text" value={this.state.text}/></p>

                <p><label>不指定value-没锁状态</label><input type="text"/></p>

            </p>

        )        

    }

}

ReactDOM.render(<Component1 />, document.getElementById('p1'));

Copy after login

This case illustrates that in React, form elements cannot be modified again after the value is given due to one-way data flow, so cooperation is required<a href="http://www.php.cn/wiki/1464.html" target="_blank"> onChange</a> event to complete. So the above case should be like this

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

class Component1 extends React.Component{

    constructor(props){

        super(props)

        this.state = {

            text: 'Hello React'

        }

    }

    change = (e) => {

        this.setState({text: e.target.value})

    }

    render(){

        return (

            <p>

                <p><label>写死value-锁定状态</label><input type="text" value="hello react" onChange={this.change}/></p>

                <p><label>动态value-没锁状态</label><input type="text" value={this.state.text} onChange={this.change}/></p>

                <p><label>不指定value-没锁状态</label><input type="text"/></p>

            </p>

        )        

    }

}

Copy after login

Effect preview

textarea element

In ordinary HTML, the textarea element is the node text value

1

2

3

<textarea>

  Hello there, this is some text in a text area

</textarea>

Copy after login

But in React, this element needs to use the value attribute.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

class Component1 extends React.Component{

    constructor(props){

        super(props)

        this.state = {

            text: 'Hello React'

        }

    }

    change = (e) => {

        this.setState({text: e.target.value})

    }

    render(){

        return (

            <p>

                <textarea value={this.state.text} onChange={this.change}/>

            </p>

        )        

    }

}

Copy after login

select element

In ordinary HTML, if the select element wants to specify the default selected value, you must add attributes to the corresponding option selected

1

2

3

4

5

6

<select>

    <option value="grapefruit">Grapefruit</option>

    <option value="lime">Lime</option>

    <option selected value="coconut">Coconut</option>

    <option value="mango">Mango</option>

</select>

Copy after login

But in React, you only need to give the attribute value

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

class Component1 extends React.Component{

    constructor(props){

        super(props)

        this.state = {

            text: 'lime'

        }

    }

    change = (e) => {

        this.setState({text: e.target.value})

    }

    render(){

        return (

            <p>

                <select value={this.state.text} onChange={this.change}>

                    <option value="grapefruit">Grapefruit</option>

                    <option value="lime">Lime</option>

                    <option value="coconut">Coconut</option>

                    <option value="mango">Mango</option>

                </select>                

            </p>

        )        

    }

}

Copy after login

input file element

because <input type="file"> is a special element, it is read-only, so you need to use ref for special processing in React

1

2

3

4

5

6

7

8

9

10

11

12

13

class Component1 extends React.Component{

    submit = (e) => {

        console.log(this.file.files)

    }

    render(){

        return (

            <p>

                <input type=&#39;file&#39; ref={input => {this.file = input}}/>

                <input type="button" value="submit" onClick={this.submit} />

            </p>

        )        

    }

}

Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the steps to select li highlighting in react

JSON and Math use case analysis in JS

The above is the detailed content of Detailed explanation of using forms in React. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of obtaining administrator rights in Win11 Detailed explanation of obtaining administrator rights in Win11 Mar 08, 2024 pm 03:06 PM

Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of division operation in Oracle SQL Detailed explanation of division operation in Oracle SQL Mar 10, 2024 am 09:51 AM

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

PHP, Vue and React: How to choose the most suitable front-end framework? PHP, Vue and React: How to choose the most suitable front-end framework? Mar 15, 2024 pm 05:48 PM

PHP, Vue and React: How to choose the most suitable front-end framework? With the continuous development of Internet technology, front-end frameworks play a vital role in Web development. PHP, Vue and React are three representative front-end frameworks, each with its own unique characteristics and advantages. When choosing which front-end framework to use, developers need to make an informed decision based on project needs, team skills, and personal preferences. This article will compare the characteristics and uses of the three front-end frameworks PHP, Vue and React.

Detailed explanation of the role and usage of PHP modulo operator Detailed explanation of the role and usage of PHP modulo operator Mar 19, 2024 pm 04:33 PM

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

Detailed explanation of the linux system call system() function Detailed explanation of the linux system call system() function Feb 22, 2024 pm 08:21 PM

Detailed explanation of Linux system call system() function System call is a very important part of the Linux operating system. It provides a way to interact with the system kernel. Among them, the system() function is one of the commonly used system call functions. This article will introduce the use of the system() function in detail and provide corresponding code examples. Basic Concepts of System Calls System calls are a way for user programs to interact with the operating system kernel. User programs request the operating system by calling system call functions

Integration of Java framework and front-end React framework Integration of Java framework and front-end React framework Jun 01, 2024 pm 03:16 PM

Integration of Java framework and React framework: Steps: Set up the back-end Java framework. Create project structure. Configure build tools. Create React applications. Write REST API endpoints. Configure the communication mechanism. Practical case (SpringBoot+React): Java code: Define RESTfulAPI controller. React code: Get and display the data returned by the API.

Detailed explanation of Linux curl command Detailed explanation of Linux curl command Feb 21, 2024 pm 10:33 PM

Detailed explanation of Linux's curl command Summary: curl is a powerful command line tool used for data communication with the server. This article will introduce the basic usage of the curl command and provide actual code examples to help readers better understand and apply the command. 1. What is curl? curl is a command line tool used to send and receive various network requests. It supports multiple protocols, such as HTTP, FTP, TELNET, etc., and provides rich functions, such as file upload, file download, data transmission, proxy

Tips for using Laravel form classes: ways to improve efficiency Tips for using Laravel form classes: ways to improve efficiency Mar 11, 2024 pm 12:51 PM

Forms are an integral part of writing a website or application. Laravel, as a popular PHP framework, provides rich and powerful form classes, making form processing easier and more efficient. This article will introduce some tips on using Laravel form classes to help you improve development efficiency. The following explains in detail through specific code examples. Creating a form To create a form in Laravel, you first need to write the corresponding HTML form in the view. When working with forms, you can use Laravel

See all articles