react基本知識(shí)

react的render函數(shù)

很多時(shí)候我們都認(rèn)為react就是render大法,也確實(shí)是,每次我們都是通過(guò)render來(lái)重新渲染頁(yè)面,我們來(lái)看render的例子:

render() {
    //JSX 語(yǔ)法
    // {}
    let helloWorld = 'yuxi';
    // array
    return (
      <span>
        <div className="App">
             helloWorld,{helloWorld};
        </div>
       
        <div>
          <States disable={false}
            handShow={this.handShow}>
            <span>hello world</span>
            <span>hello yuxi</span>
            <span>hello xiaoyu</span>
        </States>
        </div>
      </span>
    );
  }

需要注意的是:render應(yīng)該只做頁(yè)面展示和渲染,不應(yīng)該有構(gòu)造函數(shù),回調(diào)函數(shù),聲明周期等。

react的state狀態(tài)

statereact的狀態(tài)管理,react是單向數(shù)據(jù)流的,當(dāng)我們需要對(duì)數(shù)據(jù)做改變的時(shí)候應(yīng)該是:

constructor() {
    super();
    console.log('this is constructor....')
    this.state = {
      disable:false,
    }
  }
  handClick = () => this.setState(
    {
      disable: !this.state.disable
    }
  )
react的props屬性

當(dāng)我們需要把父組件的數(shù)據(jù)或者函數(shù)傳遞給子組件的時(shí)候,我們一般都使用props,我們來(lái)看一個(gè)例子:
傳遞給子組件:

     <States disable={false}
            handShow={this.handShow}>
            <span>hello world</span>
            <span>hello yuxi</span>
            <span>hello xiaoyu</span>
        </States>

子組件獲取:

 let args = this.props.disable;

我們?cè)賮?lái)比較一下reactprops,到底什么時(shí)候用哪個(gè)呢?
簡(jiǎn)單來(lái)理解:

  • 如果需要改變數(shù)據(jù)就使用:state
  • 如果需要傳遞數(shù)據(jù)就使用:props
    有一篇文章寫(xiě)得比較好:ReactJS: Props vs. State
react的聲明周期

react常用的聲明周期有:

  • constructor 構(gòu)造函數(shù)
  • componentWillMount
  • componentDidMount
  • componentWillUpdate
  • componentDidUpdate
  • componentWillUnmount
    總的來(lái)說(shuō)我們常用的就是:
    constructor,componentWillMount,componentDidMount
    來(lái)看一個(gè)簡(jiǎn)單例子:
import React, { Component } from 'react'

export class States extends Component {
  constructor() {
    super();
    console.log('this is constructor....')
    this.state = {
      disable:false,
    }
  }
  handClick = () => this.setState(
    {
      disable: !this.state.disable
    }
  )

  componentWillMount() {
  
    console.log('this is will mount')
  }

  componentDidMount() {
    console.log('this is did mount');
  }

  componentWillUpdate() {
    console.log('this is will update');
  }

  componentDidUpdate() {
    console.log('this is did update');
  }

 

  handShow = () => {
    let args = this.state.disable;
    this.props.handShow(args);
  }

  handFocus = () => {
    this.refs.myFocus.focus();
  }

  render() {
    // state practise 
    // set this.props.children
    // operate virtral DOM
    let args = this.props.children;
    let DOME = React.Children.map(args,(child) => {
        return <li>{child}</li>
    });
    console.log(DOME)
    return (
      <div>
       <input type="text" disabled={this.state.disable}></input>
       <button onClick={this.handClick}> click me</button>
       <button onClick={this.handShow}>show</button>
       <ol>
         {DOME}
       </ol>
       <input type="text" ref='myFocus'></input>
       <button onClick={this.handFocus}> focus</button>
      </div>
    )
  }
}

export default States

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

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

  • 作為一個(gè)合格的開(kāi)發(fā)者,不要只滿(mǎn)足于編寫(xiě)了可以運(yùn)行的代碼。而要了解代碼背后的工作原理;不要只滿(mǎn)足于自己的程序...
    六個(gè)周閱讀 8,679評(píng)論 1 33
  • 40、React 什么是React?React 是一個(gè)用于構(gòu)建用戶(hù)界面的框架(采用的是MVC模式):集中處理VIE...
    萌妹撒閱讀 1,190評(píng)論 0 1
  • 本筆記基于React官方文檔,當(dāng)前React版本號(hào)為15.4.0。 1. 安裝 1.1 嘗試 開(kāi)始之前可以先去co...
    Awey閱讀 7,932評(píng)論 14 128
  • HTML模版 之后出現(xiàn)的React代碼嵌套入模版中。 1. Hello world 這段代碼將一個(gè)一級(jí)標(biāo)題插入到指...
    ryanho84閱讀 6,446評(píng)論 0 9
  • 16歲的時(shí)候,他梳莫西干頭,喜歡看古惑仔,并像大哥陳浩南一樣,在學(xué)校收了一群小弟,很是有幾分威望。一個(gè)初冬的下午,...
    盧四毛閱讀 1,459評(píng)論 5 25

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