react -- Dom 與 事件

Dom節(jié)點用的比較少。但是先研究下還是有用處的,萬一哪個功能react不好實現,我們還能用自己強大的原生編程完成。

先說下 添加HTML

。哦。對這個也是比較偏門的

Code

// 插入
dangerouslySetInnerHTML={createMarkup()}
// 聲明
function createMarkup() {
  return {__html: '<h2 style="color: #ddd">HTML</h2>'};
}

事件綁定Call

一般是這樣的

  constructor(props) {
    super(props)
    this.state = {msg: 'Home Page', index: true}
        
    // This binding is necessary to make `this` work in the callback
    this.handleClick = this.handleClick.bind(this)
  }
  handleClick(e) {
    this.setState(prevState => ({
      msg: prevState.index ? 'Home Page' : 'Updata Home done',
      index: !prevState.index
    }));
  }

返回bind。。這個寫法好惡心??!
寫一個還記得,兩個,三個,每個都要復制粘貼,不專業(yè)的人看出來這是傻逼操作。

這樣改下聲明的函數:

handleClick = (e) => {
    this.setState(prevState => ({
      msg: prevState.index ? 'Home Page' : 'Updata Home done',
      index: !prevState.index
    }));
  }

不過,這樣會生成回調,每次都會重新渲染,大多數情況下是可以這樣用的,但是傳給子組件以及更下級組件的時候,就會浪費性能。

我不知道這樣翻譯對不對,官方的說法:
The problem with this syntax is that a different callback is created each time the LoggingButton renders. In most cases, this is fine. However, if this callback is passed as a prop to lower components, those components might do an extra re-rendering. We generally recommend binding in the constructor or using the property initializer syntax, to avoid this sort of performance problem.

<br />


參考鏈接: https://facebook.github.io/react/docs/refs-and-the-dom.html

--END--
<br />

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 自己最近的項目是基于react的,于是讀了一遍react的文檔,做了一些記錄(除了REFERENCE部分還沒開始讀...
    潘逸飛閱讀 3,737評論 1 10
  • It's a common pattern in React to wrap a component in an ...
    jplyue閱讀 3,397評論 0 2
  • “這樣下去有意思嗎?”小穎對阿浩說。 阿浩答不上來這樣到底有沒有意思,他聽到小穎的一聲“再見”和隨之而來的忙音。 ...
    講個故事給你聽吧閱讀 992評論 0 50
  • 1.從官網下載安裝包 https://nodejs.org/zh-cn/download 2.下載包 當前版本 n...
    火星人想回火星閱讀 242評論 0 0

友情鏈接更多精彩內容