<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 基于class關鍵字創建組件 Class組件稱之為有狀態組件。即組件的私有狀態 。 ## 使用 class 關鍵字來創建組件 在學習的過程中先書寫 如下代碼,通過報錯提示得知:在class創建的組件中,必須定義一個render函數 ``` class Hi extends React.Component{ } ``` 把render(){}函數書完完畢后,又會報錯得知:在render函數中,必須返回一個null或者符合規范的虛擬DOM元素 ``` class Hi extends React.Component{ // 通過報錯提示得知:在class創建的組件中,必須定義一個render函數 render(){ } } ``` 最終我們可以書寫成以下的代碼 ``` class Hi extends React.Component{ // 通過報錯提示得知:在class創建的組件中,必須定義一個render函數 render(){ // 在render函數中,必須返回一個null或者符合規范的虛擬DOM元素 return <div> <h1>這是用 class 關鍵字創建的組件!</h1> </div>; } } ``` ## 組件通信-父向子傳遞數據 ### 演示學習 普通構造函數定義的子組件是通過形參書寫props來接收父組件傳遞的數據。 class定義的子組件該如何接收父組件傳遞的數據?能直接按下面寫代碼? ``` class Hi extends React.Component{ render(){ return <div> <h1>你好{props.name}年齡是{props.age}</h1> </div>; } } var obj = { name:'小白', age:16 } ReactDom.render(<Hi {...obj}/>,document.getElementById('root')) ``` 肯定是不可以按照上面的方式書寫會報錯。`Uncaught ReferenceError: props is not defined`props沒有定義。 ### 正確思路 我們知道在學習Class類時候我們還有個constructor構造函數。這里面是可以傳值的。 ~~~ class Hi extends React.Component{ constructor(props){ super(props); //super寫在構造函數的第一行 } render() { return( <div>你好{this.props.name}年齡是{this.props.age} </div> ); } } ~~~ 在constructor中接收父組件傳遞的值。還要記得必須寫super(props)因為組件是繼承過來的 ## webstormJS模板 我們發現每次為了書寫一個class組件,要寫很多單詞中,一不小心就會出錯。我們可以去配置webstorm的模板。 ![](https://box.kancloud.cn/4bd35c5ba8542149700150a8bb56d9b1_1479x940.png) ### 模板文件需要先拷貝 ``` import React,{Component} from 'react'; export default class $className$ extends Component{ constructor(props){ super(props); this.state = {} } render() { return ( <div>這是$name$$end$組件</div> ); } } ``` ![](https://box.kancloud.cn/361f57d57e36416426fa4c756eeedbdf_1481x937.png) ![](https://box.kancloud.cn/ee3256a51d798dd2a91dced273b0070f_1482x937.png) ![](https://box.kancloud.cn/29277dafc86105afc5f2e54e8551ca89_1000x374.png) ![](https://box.kancloud.cn/50205046f3211403bb268a21f9bb5949_1000x374.png) ![](https://box.kancloud.cn/32d8c7db660561fcea38fd5aeb766c83_1496x946.png) ### 使用 新建tabbar.js在文件中書寫react會立即生成我們想要的Class組件。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看