A brief discussion on the principle of React two-way data binding

A brief discussion on the principle of React two-way data binding

If you have learned Vue and have a deep understanding of Vue 's two-way data binding, you will understand that the core of Vue 2.0 two-way data binding is actually to achieve data hijacking and monitoring through Object.defineProperty .

In Vue 3.0 , Proxy is used to monitor the entire object and optimize Vue2.0 .

What is two-way data binding

Two-way binding between data models and views.

When the data changes, the view also changes, and when the view changes, the data also changes synchronously; it can be said that the user's modifications to the view are automatically synchronized to the data model, and the data model also changes in the same way.

Advantages of two-way data binding: There is no need to perform CRUD (Create, Retrieve, Update, Delete) operations like one-way data binding. Two-way data binding is most commonly used on forms. In this way, when the user completes the input on the front-end page, we have obtained the user's input data and put it into the data model without any operation.

Implementing two-way data binding

However, there is no two-way data binding mechanism in React , and we need to implement it ourselves.

Data Impact View

In fact, React has helped us achieve this function by using setState({ }) method to modify data.
(The modification method provided by React internally) does not allow data modification through the method of this.state.屬性名= 數據.

Code

import React, { Component } from 'react';
// Import antd UI libraryimport { Button } from 'antd';  
class Home extends Component {
    constructor(props) {
        super(props);
        this.state = { 
            inputVal:'',
         };
    }   
    setValue=()=>{
        this.setState({
            inputVal: "Modify Value"
        })
    }
    render() {
        return (
            <div className="home" >
                Home component<p> {this.state.inputVal}</p>
                 {/* Using antd UI library*/}
                <Button type="primary" onClick={this.setValue}>Modify data</Button>
            </div>
        );
    }
}
export default Home;

Effect

insert image description here

Views affect data

onChage 監聽事件provided by React is used to realize the dynamic entry of data. At the same time, value or defaultValue is used to present the content in input box. For the convenience of display, the p tag is used here to display the content

Code

import React, { Component } from 'react';
import { Button } from 'antd'; // antd UI library class Home extends Component {
    constructor(props) {
        super(props);
        this.state = { 
            inputVal:'',
         };

    }    
    change = (ev)=>{
        this.setState({
            inputVal:ev.target.value
        })
    }
    render() {
        return (
            <div className="home" >
                Home component <input 
                    onChange={this.change}
                    // value={this.state.inputVal}
                    defaultValue={this.state.inputVal}
                    placeholder="Enter text content"
                 />
                <p>&emsp;{this.state.inputVal}</p>
            </div>
        );
    }
}
export default Home;

Effect

insert image description here

Notice:

When using value binding, only one value and defaultValue can be used, otherwise a warning will be reported

This concludes this article on the principle of React two-way data binding. For more relevant React two-way binding content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Two-way binding problem between parent component and child component in react
  • A brief analysis of the principle of two-way data binding in Vue, Angular and React
  • React's detailed explanation of how to implement two-way binding using plugins and without plugins
  • React implements two-way binding example code
  • Do you really understand two-way binding in react?

<<:  Detailed steps for installing and using vmware esxi6.5

>>:  How to rename the table in MySQL and what to pay attention to

Recommend

jQuery implements the bouncing ball game

This article shares the specific code of jQuery t...

Sample code for seamless scrolling with flex layout

This article mainly introduces the sample code of...

Sample code for configuring nginx to support https

1. Introduction Are you still leaving your websit...

MySQL series 9 MySQL query cache and index

Table of contents Tutorial Series 1. MySQL Archit...

JavaScript basics of this pointing

Table of contents this Method In the object Hidde...

CSS uses BEM naming convention practice

When you see a class, what information do you wan...

Example to explain the size of MySQL statistics table

Counting the size of each table in each database ...

Achieve 3D flip effect with pure CSS3 in a few simple steps

As a required course for front-end developers, CS...

React hooks introductory tutorial

State Hooks Examples: import { useState } from &#...

Disable input text box input implementation properties

Today I want to summarize several very useful HTML...

Mysql sorting to get ranking example code

The code looks like this: SELECT @i:=@i+1 rowNum,...

Native JS to implement click number game

Native JS implements the click number game for yo...

MySQL multi-table join introductory tutorial

Connections can be used to query, update, and est...

Solve the problem of combining AND and OR in MySQL

As shown below: SELECT prod_name,prod_price FROM ...