If you have learned In What is two-way data bindingTwo-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 Data Impact View In fact, 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 Views affect data 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> {this.state.inputVal}</p> </div> ); } } export default Home; Effect Notice: When using 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:
|
<<: Detailed steps for installing and using vmware esxi6.5
>>: How to rename the table in MySQL and what to pay attention to
This article shares the specific code of jQuery t...
This article uses examples to describe MySQL pess...
This article mainly introduces the sample code of...
1. Introduction Are you still leaving your websit...
Table of contents Tutorial Series 1. MySQL Archit...
Table of contents this Method In the object Hidde...
When you see a class, what information do you wan...
Counting the size of each table in each database ...
As a required course for front-end developers, CS...
State Hooks Examples: import { useState } from ...
Today I want to summarize several very useful HTML...
The code looks like this: SELECT @i:=@i+1 rowNum,...
Native JS implements the click number game for yo...
Connections can be used to query, update, and est...
As shown below: SELECT prod_name,prod_price FROM ...