06 事件处理函数绑定与事件对象
生活随笔
收集整理的這篇文章主要介紹了
06 事件处理函数绑定与事件对象
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
事件處理函數綁定
- DOM事件處理 addEventListener or onclick = function(){} 純小寫
- React元素也采用了類似DOM0標準中的事件屬性定義的方法 小駝峰
阻止a標簽默認行為
- a標簽錨點
- 使用偽協議在React中會報warning
- 阻止默認事件 e(React事件對象,并非js原生的e)
事件對象
- SyntheticBaseEvent 合成基礎事件對象
- 這個SBE是遵守W3C事件對象的規范的,不存在任何的瀏覽器兼容性問題
React為什么要將事件處理直接綁定在元素上
- React認為事件處理和視圖有直接關系,寫在一起可以更直觀地表述視圖與邏輯的關系,便于維護
this指向
- ES6 class模塊默認不對事件處理函數進行this的再綁定
- DOM0事件
解決this指向 建議使用134
注意:給子組件的屬性傳遞函數時,由于每次都創建一個回調,子組件每次都接收一個新的函數,可能觸發子組件的render 顯式傳入e
事件對象都在最后一個參數
// 在構造器中bind this class MyButton extends React.Component {constructor(props) {super(props)this.handleClick = this.handleClick.bind(this)}handleClick() {console.log('this', this)// this指向類MyButton}render() {return <button onClick={this.handleClick}>按鈕</button>} } // 在視圖中bind this class MyButton extends React.Component {constructor(props) {super(props)}handleClick(e) {// bind方法要使用e,無須傳入,是隱式傳入的 console.log('this', this, e)// this指向類MyButton}render() {return <button onClick={this.handleClick.bind(this)}>按鈕</button>} } // 回調 + 箭頭函數 + 方法執行 class MyButton extends React.Component {constructor(props) {super(props)}handleClick(e) {// 要使用e則需要傳入econsole.log('this', this)// this指向類MyButton}render() {return <button onClick={(e) => this.handleClick(e)}>按鈕</button>} }- 用在子組件上時,父組件每次render都創建一個新的回調,fn是響應的,會觸發子組件的render
總結
以上是生活随笔為你收集整理的06 事件处理函数绑定与事件对象的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mvc怎么单独引用css文件,关于asp
- 下一篇: vb红绿灯自动切换_VB红绿灯程序