<React篇> React.Component

? ? ? ? ? ?一直對React心儀之久,也就在今天終于拿起筆桿攒读,對React相關用法進行“筆伐”恢暖。

? ? ? ? ? ? 先聲明:本文的來源與參考:(一)facebook ?對React.Component的文檔介紹;(二)可岂,網上相關開發(fā)者實踐經驗介紹岸售。?

? ? ? ? ? ? 首先React.Component is provided by React.

? ? ? ? ? ? 關于React.Component官方文檔是這樣的介紹:Components let you splite the UI into independent,reusable pieces,and think about each piece in isolation.

? ? ? ? ? ? 首先要明確践樱,React.Component幾乎不直接當做組件使用,一般是“繼承”它凸丸,自定義組件拷邢,當然自定義組件至少要實現(xiàn)render()方法。

? ? ? ? ? ? 例如:

? ? ? ? ? ? class ?Greeting extends React.Component{

? ? ? ? ? ? ? ? ? ? ?render(){

? ? ? ? ? ? ? ? ? ? ? ? ? ? ?return <h1>Hello,{this.props.name} </h1>

? ? ? ? ? ? ? ? ? ? ? ? ?}?

? ? ? ? ? ? ?}

? ? ? ? ?然后屎慢,咱們來看一看The Component Lifecycle.

? ? ? ? ?官方對其的解析為:Each component ?has several "lifecycle methods" that you can override to run code at particular times in the process.Methods prefixed with will are called right before something happens,and methods prefixed with did are called right after something happens.(大概意思就是:每個組件都有若干的生命周期方法瞭稼,你可以在特定時間重寫這些方法,以至于在它的生命周期特定進程里面調用腻惠。前綴will的方法在該事件之前調用环肘。前綴did的方法在該事件之后調用)。

? ? ? ? 看完解釋之后集灌,接下來就要好好熟悉Component的生命周期中的各個函數(shù)的用法及場景啦悔雹。

場景:No.1

? ? ? ? ?簡稱:Mounting。

? ? ? ? ? 英文解釋為:These methods are called when an instance of a component is being created and inserted into the DOM.

? ? ? ? (1).constructor()

? ? ? ? 文檔解釋:the constructor for a React component is called before it is mouted.The constrctor is the right place to initialize state.

? ? ? ? ?Here's an example of a valid React.Component subclass constructor:

? ? ? ? ?constructor(props){

? ? ? ? ? ? ? ? ?super(props)

? ? ? ? ? ? ? ? ? this.state = {color:this.initalColor};

? ? ? ? }

? ? ? ? ? (2)componentWillMount()

? ? ? ? ? 摘至文檔:componentWillMount() is invoked immediately before mounting occurs.it is called before render().This is the only lifecycle hook called on server rendering.

注:建議用constructor()代替componentWillMount().

? ? ? (3)render()

? ? ? ? ? 摘至文檔:when called ,it should examine this.props and this.state and return a single React element.you can also return null or false to indicate that you don't want anything rendered.when returning null or false,ReactDom.findDOMNode(this) will return null;the render() function should be pure,meaning that it does not modify component state,it return the same result each time it's invoked,and it does not directly interact with the browser.if you need to interact with the browser,perform your work in componentDidMount() or the other lifecycle methods instead.keeping render() pure makes components easier to think about.

注意:render() will not be invoked if shouldComponentUpdate return false;如果shouldComponentUpdate返回false,render()將不會被調用欣喧。

? ? ? ? ?(4)componentDidMount

? ? ? ? 摘至文檔:componentDidMount() is invoked immediately after a component is mounted,Initialization that requires DOM nodes should go here. if you need to load data from a remote endpoint,this is a good place to instantiate the network request.Setting state in this method will trigger a re-rending.

? ? ?No.2

? ? ?簡稱:Updating

? ? ? ? an ?update can be caused by changes to props or state.these ?methods are called when a component is being re-rendered. (通過對props或者state的變化腌零,會引起組件的更新,這些方法會被調用當一個組件正在被繪制的時候)续誉。

? ? ? ?(1)componentWillReceiveProps(nextPropd)

? ? ? ?(2)shouldComponentUpdate(nextProps,nextState)

? ? ? ?(3)componentWillUpdate()

? ? ? ?(4)render()

? ? ? ?(5)componentDidUpdate()

? ? ? No.3

? ? ? 簡稱:Unmounting

? ? ? ? ?this method is called when a component is being removed from the DOM:(當組件正從DOM中移除的時候這個方法將會被調用)莱没。

? ? ? ? ?(1)componentWillUnmount()

? ? ? 介紹完React.Component生命周期初肉,再來看看React.Component其他的APIS:

? ? ? ? (1)each component also provides some other APIs:

? ? ? ? ? ? ? ? ?setState();

? ? ? ? ? ? ? ? ?forceUpdate()

? ? ? ?(2)Class Properties

? ? ? ? ? ? ? ? ? defaultProps

? ? ? ? ? ? ? ? ? displayName

? ? ? ?(3)Instance Properties

? ? ? ? ? ? ? ? ? ? props

? ? ? ? ? ? ? ? ? ? state

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末酷鸦,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子牙咏,更是在濱河造成了極大的恐慌臼隔,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,542評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件妄壶,死亡現(xiàn)場離奇詭異摔握,居然都是意外死亡,警方通過查閱死者的電腦和手機丁寄,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,596評論 3 385
  • 文/潘曉璐 我一進店門氨淌,熙熙樓的掌柜王于貴愁眉苦臉地迎上來泊愧,“玉大人,你說我怎么就攤上這事盛正∩驹郏” “怎么了?”我有些...
    開封第一講書人閱讀 158,021評論 0 348
  • 文/不壞的土叔 我叫張陵豪筝,是天一觀的道長痰滋。 經常有香客問我,道長续崖,這世上最難降的妖魔是什么敲街? 我笑而不...
    開封第一講書人閱讀 56,682評論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮严望,結果婚禮上多艇,老公的妹妹穿的比我還像新娘。我一直安慰自己像吻,他們只是感情好墩蔓,可當我...
    茶點故事閱讀 65,792評論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著萧豆,像睡著了一般奸披。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上涮雷,一...
    開封第一講書人閱讀 49,985評論 1 291
  • 那天阵面,我揣著相機與錄音,去河邊找鬼洪鸭。 笑死样刷,一個胖子當著我的面吹牛,可吹牛的內容都是我干的览爵。 我是一名探鬼主播置鼻,決...
    沈念sama閱讀 39,107評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼蜓竹!你這毒婦竟也來了箕母?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 37,845評論 0 268
  • 序言:老撾萬榮一對情侶失蹤俱济,失蹤者是張志新(化名)和其女友劉穎嘶是,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蛛碌,經...
    沈念sama閱讀 44,299評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡聂喇,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,612評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了蔚携。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片希太。...
    茶點故事閱讀 38,747評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡克饶,死狀恐怖,靈堂內的尸體忽然破棺而出誊辉,到底是詐尸還是另有隱情彤路,我是刑警寧澤,帶...
    沈念sama閱讀 34,441評論 4 333
  • 正文 年R本政府宣布芥映,位于F島的核電站洲尊,受9級特大地震影響,放射性物質發(fā)生泄漏奈偏。R本人自食惡果不足惜坞嘀,卻給世界環(huán)境...
    茶點故事閱讀 40,072評論 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望惊来。 院中可真熱鬧丽涩,春花似錦、人聲如沸裁蚁。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,828評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽枉证。三九已至矮男,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間室谚,已是汗流浹背毡鉴。 一陣腳步聲響...
    開封第一講書人閱讀 32,069評論 1 267
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留秒赤,地道東北人猪瞬。 一個月前我還...
    沈念sama閱讀 46,545評論 2 362
  • 正文 我出身青樓,卻偏偏與公主長得像入篮,于是被迫代替她去往敵國和親陈瘦。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 43,658評論 2 350

推薦閱讀更多精彩內容

  • In this article we will discuss how to use Higher Order C...
    人頭原子彈閱讀 559評論 0 0
  • 感覺最近接觸了信息才雜了潮售,沒有任何時候任何一件事能進入心流狀態(tài)痊项。偶爾,自己在寫文章的時候會有饲做。心流线婚,如何進入遏弱? 首...
    聞舒閱讀 179評論 0 0
  • 13.丁香 內景盆均,日,成家公寓 次日清晨起來漱逸,蘇乞兒拉開藍色的窗簾泪姨,一抹陽光透進來游沿,這種恬靜讓人難以想象這是民國亂...
    蘇乞兒1閱讀 318評論 0 0
  • 研究一下回調函數(shù),回調函數(shù)的英文名:callback ?????? 函數(shù)也是對象肮砾,看一下書面化的說明: 1诀黍、函數(shù)是用Fu...
    陳川Lethe閱讀 275評論 0 0