react入坑指南

本篇為萌新篇.

  • 1.定義的類名必須以大寫字母開頭,如:
var DemoFunction = React.createClass({ });

就不能寫成

var demoFunction = React.createClass({ });
  • 2.組件類中只能包含一個頂層標(biāo)簽,如:
var DemoFunction = React.createClass({
  render: function() {
    return (
        <div class="topdiv">
          <span>Hello React!</span>
          <span>Hello React Again!</span>
        </div>
      );
  }
});

不能寫成

var DemoFunction = React.createClass({
  render: function() {
    return (
        <div class="topdiv">
          <span>Hello React!</span>
          <span>Hello React Again!</span>
        </div>
        <div class="topdiv2">
          <span>Hello React!</span>
          <span>Hello React Again!</span>
        </div>
      );
  }
});
  • 3.接上條,其實可以調(diào)用dangerouslySetInnerHtml()方法輸出HTML的原本內(nèi)容,這樣可以避免react對標(biāo)簽等的限制,如下例(根據(jù)官方文檔的編碼習(xí)慣修改,可能這就是所謂的模塊化和組件化思想?):
var DemoFunction = React.createClass({
  htmlContent: function() {
    var html = "<div class='topdiv'><span>Hello React!</span><span>Hello React Again!</span></div><div class='topdiv2'><span>Hello React!</span><span>Hello React Again!</span></div>";
    return {__html: html};
  },
  render: function() {
    return (
        <div dangerouslySetInnerHTML={this.htmlContent()}></div>
      );
  }
});

這里需要注意的是:
1.聲明的HTML字符串不能有換行等,否則將不能被解析;
2.根據(jù)官方文檔的說明,使用這個方法時必須注意來自外部的XSS攻擊:

dangerouslySetInnerHTML is React's replacement for using innerHTML in the browser DOM. In general, setting HTML from code is risky because it's easy to inadvertently expose your users to a cross-site scripting (XSS) attack. So, you can set HTML directly from React, but you have to type out dangerouslySetInnerHTML and pass an object with a __html key, to remind yourself that it's dangerous.

另外,個人建議把return方法用小括號( )包起來;

  • 4.react不提供形如$.ajax()之類的方法,而引入JQuery將不可避免的使得頁面變得臃腫,因而我們可以嘗試使用fetch來解決該問題:
fetch("https://api.xxxx.xxx/xxx").then(function(response){
  console.log(response);
});

關(guān)于fetch
需要注意的是:
1.根據(jù)MDN的Browser compatibility,fetch的瀏覽器支持情況需要被慎重考慮,引用es6-promise.js可以解決部分老舊瀏覽器的兼容性問題;
2.fetch與react本身無關(guān),是基于es6實現(xiàn),返回的是promise對象;

The fetch() method takes one mandatory argument, the path to the resource you want to fetch. It returns a promise that resolves to the Response
to that request, whether it is successful or not. You can also optionally pass in an init options object as the second argument (see Request
).

3.每次進(jìn)行fetch會話的時候默認(rèn)會創(chuàng)建新的session key, 在服務(wù)端同步數(shù)據(jù)的時候需要注意;
4.注意跨域問題;

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

相關(guān)閱讀更多精彩內(nèi)容

  • 現(xiàn)在最熱門的前端框架,毫無疑問是 React 。上周,基于 React 的 React Native 發(fā)布,結(jié)果一...
    sakura_L閱讀 488評論 0 0
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,118評論 0 23
  • 對抗歲月的侵襲, 除了容顏的加持, 更需要內(nèi)在力量的修養(yǎng)... 《楚辭·九歌》 ...
    墨影teresa閱讀 2,120評論 22 52
  • 閱讀書目:顛覆平庸Part1-5 閱讀時間:45分鐘(番茄鐘) 閱讀困難:時間預(yù)測不是很準(zhǔn)確,計劃一個番茄鐘,實際...
    濱鴻閱讀 256評論 0 0

友情鏈接更多精彩內(nèi)容