詳解react關(guān)于事件綁定this的四種方式
在react組件中,每個(gè)方法的上下文都會(huì)指向該組件的實(shí)例,即自動(dòng)綁定this為當(dāng)前組件,而且react還會(huì)對這種引用進(jìn)行緩存,以達(dá)到cpu和內(nèi)存的最大化。在使用了es6 class或者純函數(shù)時(shí),這種自動(dòng)綁定就不復(fù)存在了,我們需要手動(dòng)實(shí)現(xiàn)this的綁定
React事件綁定類似于DOM事件綁定,區(qū)別如下:
1.React事件的用駝峰法命名,DOM事件事件命名是小寫
2.通過jsx,傳遞一個(gè)函數(shù)作為event handler,而不是一個(gè)字符串。
3.React事件不能通過返回false來阻止默認(rèn)事件,需要顯式調(diào)用preventDefault()
如下實(shí)例:
<a href="#" onclick="console.log('The link was clicked.'); return false"> Click me </a> class ActionLink extends React.Component { constructor(props) { super(props); } handleClick(e) { e.preventDefault(); console.log('The link was clicked.'); } render() { return ( <a href="#" onClick={this.handleClick.bind(this)}>Click Me...</a> ); } }
ps:React組件類的方法沒有默認(rèn)綁定this到組件實(shí)例,需要手動(dòng)綁定。
以下是幾種綁定的方法:
bind方法
直接綁定是bind(this)來綁定,但是這樣帶來的問題是每一次渲染是都會(huì)重新綁定一次bind;
class Home extends React.Component { constructor(props) { super(props); this.state = { }; } del(){ console.log('del') } render() { return ( <div className="home"> <span onClick={this.del.bind(this)}></span> </div> ); } }
構(gòu)造函數(shù)內(nèi)綁定
在構(gòu)造函數(shù) constructor 內(nèi)綁定this,好處是僅需要綁定一次,避免每次渲染時(shí)都要重新綁定,函數(shù)在別處復(fù)用時(shí)也無需再次綁定
class Home extends React.Component { constructor(props) { super(props); this.state = { }; this.del=this.del.bind(this) } del(){ console.log('del') } render() { return ( <div className="home"> <span onClick={this.del}></span> </div> ); } }
::不能傳參
如果不傳參數(shù)使用雙冒號也是可以
class Home extends React.Component { constructor(props) { super(props); this.state = { }; } del(){ console.log('del') } render() { return ( <div className="home"> <span onClick={::this.del}></span> </div> ); } }
箭頭函數(shù)綁定
箭頭函數(shù)不僅是函數(shù)的'語法糖',它還自動(dòng)綁定了定義此函數(shù)作用域的this,因?yàn)槲覀儾恍枰賹λ鼈冞M(jìn)行bind方法:
class Home extends React.Component { constructor(props) { super(props); this.state = { }; } del=()=>{ console.log('del') } render() { return ( <div className="home"> <span onClick={this.del}></span> </div> ); } }
以上幾種方法都可以實(shí)現(xiàn)this綁定,使用那種各自的習(xí)慣;希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
react中form.setFieldvalue數(shù)據(jù)回填時(shí) value和text不對應(yīng)的問題及解決方法
這篇文章主要介紹了react中form.setFieldvalue數(shù)據(jù)回填時(shí) value和text不對應(yīng)的問題及解決方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07React項(xiàng)目仿小紅書首頁保姆級實(shí)戰(zhàn)教程
React 是一個(gè)用于構(gòu)建用戶界面的 Javascript庫,接下來將通過實(shí)戰(zhàn)小紅書首頁的詳細(xì)介紹其設(shè)計(jì)思路和方法,將讀者帶入到react的開源世界,需要的朋友可以參考下2022-07-07React Native react-navigation 導(dǎo)航使用詳解
本篇文章主要介紹了React Native react-navigation 導(dǎo)航使用詳解,詳解的介紹了react-navigation導(dǎo)航的使用,具有一定的參考價(jià)值,有興趣的可以了解一下2017-12-12React之如何在Suspense中優(yōu)雅地請求數(shù)據(jù)
Suspense 是 React 中的一個(gè)組件,直譯過來有懸掛的意思,能夠?qū)⑵浒漠惒浇M件掛起,直到組件加載完成后再渲染,本文詳細(xì)介紹了如何在Suspense中請求數(shù)據(jù),感興趣的小伙伴可以參考閱讀本文2023-04-04react時(shí)間分片實(shí)現(xiàn)流程詳解
實(shí)現(xiàn)react時(shí)間分片,主要內(nèi)容包括什么是時(shí)間分片、為什么需要時(shí)間分片、實(shí)現(xiàn)分片開啟 - 固定、實(shí)現(xiàn)分片中斷、重啟 - 連續(xù)、分片重啟、實(shí)現(xiàn)延遲執(zhí)行 - 有間隔、時(shí)間分片異步執(zhí)行方案的演進(jìn)、時(shí)間分片簡單實(shí)現(xiàn)、總結(jié)、基本概念、基礎(chǔ)應(yīng)用、原理機(jī)制和需要注意的事項(xiàng)等2022-11-11使用react-beautiful-dnd實(shí)現(xiàn)列表間拖拽踩坑
相比于react-dnd,react-beautiful-dnd更適用于列表之間拖拽的場景,本文主要介紹了使用react-beautiful-dnd實(shí)現(xiàn)列表間拖拽踩坑,感興趣的可以了解一下2021-05-05