react監聽input框里的值


import React, { Component } from 'react'
class App extends Component {
  constructor(props) {
    super(props)
    //this.state = {}  定義數據
    this.state = {
      inputValue: ""
    }
  }
  render() {
    return (
      <div>
        {/* react里使用表達式,需要用大括號 */}
        {/* react里綁定事件用駝峰命名,如,onChange */}
        {/* react里需要改變this的指向,用bind(this) 把this指向到這個標簽里 */}
        <input type="text" value={this.state.inputValue} onChange={this.change.bind(this)} />
      </div>
    )
  }

  change(e) {
    console.log(e.target.value);
    //設置數據的值,用this.setState({})
    this.setState({
      inputValue: e.target.value
    })
  }
}
export default App;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM