# 基本組件(構造函數)
## 定義組件和使用組件基礎語法
**通過構造函數定義組件**
`function Name(){ return null }`
**使用組件**
`<Name></name>`
## 案例
~~~
function Hello() {
return <div>
這是一個div
<h1>這是一個大大的h1</h1>
</div>;
}
ReactDOM.render(<Hello></Hello>, document.getElementById('app'))
~~~
<iframe width="100%" height="300" src="//jsrun.net/6FyKp/embedded/all/light" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
# 注意事項
## 組件返回值
~~~
import React from 'react';
import ReactDOMfrom 'react-dom'
?
function Hello() {
?
}
ReactDOM.render(<Hello ></Hello>, document.getElementById('app'))
~~~
~~~
Uncaught Invariant Violation: Hello(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null.
~~~
Hello組件。沒有從呈現返回任何內容。這通常意味著缺少返回語句。或者,若不呈現任何內容,則返回空值
## 在定義組件時組件名稱必須以大寫開頭。
因為是構造函數。如果小寫會報以下錯誤:
~~~
import React from 'react';
import ReactDOMfrom 'react-dom'
function hello() {
?return <div>
? 你好
?</div>;
}
ReactDOM.render(<hello ></hello>, document.getElementById('app'))
~~~
這個hello標簽瀏覽器中不存在,如果你想用React組件,那么請用首字母大寫開頭。
~~~
react-dom.development.js:506 Warning: The tag <hello> is unrecognized in this browser. If you meant to render a React component, start its name with an uppercase letter.in hello
~~~
## 總結
定義普通組件,我們可以認為是構造函數。首字母大寫。且構造函數中必須手動返回結構如果沒有結構手動返回null;
- webpack復習
- React基礎
- 前端三大主流框架對比
- React中幾個核心的概念
- React基礎語法
- React JSX語法
- React組件
- 普通組件
- 組件通信-父向子傳遞
- 組件拆成單個文件
- 面向對象復習
- Class組件基礎
- Class組件的私有狀態(私有數據)
- 案例:實現評論列表組件
- 組件樣式管理
- 組件樣式分離-樣式表
- CSS模塊化
- 生命周期
- React組件生命周期
- Counter組件來學習組件生命周期
- 生命周期總結
- 生命周期案例
- React評論列表
- React雙向數據綁定
- React版todolist
- 其它提高(了解)
- 組件默認值和數據類型驗證
- 綁定this并傳參的三種方式
- 祖孫級和非父子組件傳遞數據(了解)
- React路由
- 路由基礎
- 動態路由
- 路由嚴格模式
- 路由導航定位
- 路由重定向
- 路由懶加載
- WolfMovie項目
- 項目初始化
- AntDesign使用
- 其它相關了解