import React,{Component} from 'react' import ReactDOM from 'react-dom' class App extends Component{ //給一個初始化狀態 constructor(){ super(); this.state={ result:0 } } handChange=()=>{ //獲取input a的類容,這個a就是咱們給的標識ref="a" let a=this.refs.a.value||0; let b=this.refs.b.value||0; //改變狀態,更新視圖 this.setState({result:parseFloat(a)+parseFloat(b)}) } render(){ return ( <div> <input type="text" ref="a" onChange={this.handChange}/> + <input type="text" ref="b" onChange={this.handChange}/> <p>{this.state.result}</p> </div> ) } } ReactDOM.render(<App/>,document.querySelector("#root"))