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

Native js to achieve simple carousel effect

This article shares the specific code of js to ac...

In-depth understanding of the use of r2dbc in MySQL

Introduction MySQL should be a very common databa...

Detailed explanation of some settings for Table adaptation and overflow

1. Two properties of table reset: ①border-collaps...

How to view the storage location of MySQL data files

We may have a question: After we install MySQL lo...

Summary of the top ten problems of MySQL index failure

Table of contents background 1. The query conditi...

Use nginx to dynamically convert image sizes to generate thumbnails

The Nginx ngx_http_image_filter_module module (ng...

MySQL 8.0.16 winx64 installation and configuration method graphic tutorial

I just started learning about databases recently....

The pitfalls of deploying Angular projects in Nginx

Searching online for methods to deploy Angular pr...

Implementation of drawing audio waveform with wavesurfer.js

1. View the renderings Select forward: Select bac...

How to install and configure WSL on Windows

What is WSL Quoting a passage from Baidu Encyclop...

JavaScript implements password box verification information

This article example shares the specific code of ...

What can I use to save you, my table (Haiyu Blog)

Tables once played a very important role in web p...

WeChat applet realizes the effect of swiping left to delete list items

This article shares the specific code for WeChat ...