React简单实现双向数据绑定
程序员文章站
2023-12-23 23:28:51
import React, { Component } from 'react' import ReactDOM from 'react-dom' class App extends Component { constructor(props) { super(props); this.state ... ......
import react, { component } from 'react' import reactdom from 'react-dom' class app extends component { constructor(props) { super(props); this.state = { val:"react的双向数据绑定" } } change=(event)=>{ this.setstate({val:event.target.value}) } render() { return ( <div> <input type="text" value={this.state.val} onchange={this.change} /> {this.state.val} </div> ); } } reactdom.render(<app/>,window.root) /* event 事件对象 event.target 事件源 利用了event.target这个事件源去更新react中的数据状态 */