单项数据绑定 在 Vue 中,可以通过 v-model 指令来实现双向数据绑定。但是,在 React 中并没有指令的概念,而且 React 默认不支持 双向数据绑定 。
React 只支持,把数据从 state 上传输到 页面,但是,无法自动实现数据从 页面 传输到 state 中 进行保存。
React中,只支持单项数据绑定,不支持双向数据绑定。不信的话,我们来看下面这个例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import React from "react" ;export default class MyComponent extends React .Component { constructor(props) { super (props); this .state = { msg: "这是 MyComponent 组件 默认的msg" }; } render() { return ( <div> <h3>呵呵哒</h3> <input type="text" value={this .state.msg} /> </div> ); } }
上方代码中,我们尝试在 input文本框中读取 state.msg 的值,运行结果中,却弹出了警告:
20190213_2000.png
1 Warning: Failed prop type: You provided a `value` prop to a form field without an `onChange` handler. This will render a read-only field. If the field should be mutable use `defaultValue`. Otherwise, set either `onChange` or `readOnly`.
通过onChange方法,实现双向数据绑定 如果针对 表单元素做 value 属性绑定,那么,必须同时为 表单元素 绑定 readOnly, 或者提供 onChange 事件:
绑定readOnly的举例如下:(表示value中的数据是只读的)
1 <input type="text" value={this .state.msg} readOnly />
绑定 onChange 的举例如下 :(通过onChange方法,实现双向数据绑定)
(1)index.html:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" > <meta name ="viewport" content ="width=device-width, initial-scale=1.0" > <meta http-equiv ="X-UA-Compatible" content ="ie=edge" > <title > Document</title > </head > <body > <div id ="app" > </div > </body > </html >
(2)main.js:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import React from "react" ;import ReactDOM from "react-dom" ;import MyComponent from "./components/MyComponent.jsx" ;ReactDOM.render( <div> <MyComponent></MyComponent> </ div>, document .getElementById("app" ) );
(3)components/MyComponent.jsx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 import React from "react" ;export default class MyComponent extends React .Component { constructor (props) { super (props); this .state = { msg: "这是组件 默认的msg" }; } render() { return ( <div> <h1>呵呵哒</h1> <input type="text" value={this.state.msg} onChange={this.txtChanged} ref="txt" / > <h3>{"实时显示msg中的内容:" + this .state.msg}</h3> </ div> ); } txtChanged = (e ) => { this .setState({ msg: e.target.value }); }; }
工程文件: