自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

React 入門第五步:組件布局小案例

開發(fā) 前端
從接觸組件這個概念,到組件間的傳遞,我們學(xué)習(xí)了很多內(nèi)容,也深刻的體會了 JSX 就是 JS 這個概念。而在組件間數(shù)據(jù)傳遞部分,Props 是核心關(guān)鍵點。

我們結(jié)合前面學(xué)習(xí)的內(nèi)容,做一個界面布局的小案例,案例最終呈現(xiàn)的效果如下:

基本布局

CSS 樣式已經(jīng)提前寫好了,放在根路徑下,起名為 style.css,具體代碼如下:

  1. html, body { height: 100%; } 
  2.  
  3. body { margin: 0; padding: 0; } 
  4.  
  5. .header, .footer { 
  6.   width: 100%; height: 80px; position: absoluteleft: 0; 
  7.   line-height: 80px; text-align: center; font-size: 18px; 
  8.   color: #fff; 
  9.  
  10. .header { background-color: skyblue; top: 0; } 
  11.  
  12. .footer { background-color: purple; bottom: 0; } 
  13.  
  14. .main { 
  15.   width: 100%; position: absolutetop: 80px; bottom: 80px; 
  16.   padding: 20px 0; text-align: center; background: khaki; 

我們創(chuàng)建好對應(yīng)的子組件,分別為 src\Components\Header.js、src\Components\Home.js、src\Components\Footer.js。

在 \src\App.js 中引入上面的三個子組件及樣式文件:

  1. import React, { Component } from 'react' 
  2.  
  3. import Header from './Components/Header' 
  4. import Home from './Components/Home' 
  5. import Footer from './Components/Footer' 
  6.  
  7. import './style.css' 
  8.  
  9. export class App extends Component { 
  10.   render() { 
  11.     return ( 
  12.       <> 
  13.         <Header></Header> 
  14.         <Home></Home> 
  15.         <Footer></Footer> 
  16.       </> 
  17.     ) 
  18.   } 
  19.  
  20. export default App 

 然后在子組件中,分別寫好初始代碼,因為已經(jīng)有了對應(yīng)的樣式代碼,因此在子組件中直接使用即可:

  1. // src\Components\Header.js   
  2. import React from 'react' 
  3.  
  4. function Header() { 
  5.   return ( 
  6.     <> 
  7.       <div className={'header'}> 
  8.         Header 
  9.       </div> 
  10.     </> 
  11.   ) 
  12.  
  13. export default Header 
  14.  
  15. // ================================================= 
  16. // src\Components\Home.js  
  17. import React from 'react' 
  18.  
  19. function Home() { 
  20.   return ( 
  21.     <> 
  22.       <div className={'main'}> 
  23.         Home 
  24.       </div> 
  25.     </> 
  26.   ) 
  27.  
  28. export default Home 
  29.  
  30. // ================================================= 
  31. // src\Components\Footer.js 
  32. import React from 'react' 
  33.  
  34. function Footer() { 
  35.   return ( 
  36.     <> 
  37.     <div className={'footer'}> 
  38.         Footer 
  39.       </div> 
  40.     </> 
  41.   ) 
  42.  
  43. export default Footer 

 基礎(chǔ)代碼實現(xiàn)之后,我們就能看到對應(yīng)的效果了。但是,按照正常的編碼邏輯,header 和 footer 應(yīng)該屬于公共部分,中間部分無論展示什么內(nèi)容,頭和尾都是需要展示的。

集成公共組件

我們需要將 Header 和 Footer 作為公共組件使用,需要添加一個src\Components\Layout.js 組件,Layout 組件的作用就是將 header與 footer 顯示出來,同時把中間的 main 內(nèi)容空出來,將來我們傳入什么樣的 JSX ,那么 main 里就顯示什么樣的 DOM。

在 Layout.js 中,引入 Header 與 Footer 組件并應(yīng)用在 JSX 中,然后利用 JSX 傳參,展示主體內(nèi)容。前面我們已經(jīng)學(xué)習(xí)過,這里使用的是函數(shù)組件,只需要在函數(shù)中使用形參獲取 Props ,然后在展示主體內(nèi)容的地方獲取 {props.children} 就可以了:

  1. import React from 'react' 
  2. import Header from './Header' 
  3. import Footer from './Footer' 
  4.  
  5. function Layout(props) { 
  6.   return ( 
  7.     <> 
  8.       <Header /> 
  9.       <div className="main"
  10.         {props.children} 
  11.       </div> 
  12.       <Footer /> 
  13.     </> 
  14.   ) 
  15.  
  16. export default Layout 

 

 公共組件完成后,我們想展示那個主體組件的內(nèi)容,就在那個組件中引入 Layout ,然后將主體內(nèi)容以傳遞 jsx 的方式傳入就可以了,最后再在 App 中,引入主體組件進行展示,具體代碼如下:

  1. // Home.js  
  2. import React from 'react' 
  3. import Layout from './Layout' 
  4.  
  5. function Home() { 
  6.   return ( 
  7.     <Layout> 
  8.       <div>Home</div> 
  9.     </Layout> 
  10.   ) 
  11.  
  12. export default Home 
  13.  
  14. // ==================================== 
  15. // List.js  
  16. import React from 'react' 
  17. import Layout from './Layout' 
  18.  
  19. function List() { 
  20.   return ( 
  21.     <Layout> 
  22.       <div className="main"
  23.         list 
  24.       </div> 
  25.     </Layout> 
  26.   ) 
  27.  
  28. export default List 
  29.  
  30. // ==================================== 
  31. // App.js  
  32. import React, { Component } from 'react' 
  33. import Home from './Components/Home' 
  34. import List from './Components/List' 
  35.  
  36. import './style.css' 
  37.  
  38. export class App extends Component { 
  39.   render() { 
  40.     return ( 
  41.       <> 
  42.         <Home /> 
  43.         <List /> 
  44.       </> 
  45.     ) 
  46.   } 
  47.  
  48. export default App 

總的來說,就是利用了向組件傳遞 JSX 的特性實現(xiàn)的。

最后,做個小總結(jié),從接觸組件這個概念,到組件間的傳遞,我們學(xué)習(xí)了很多內(nèi)容,也深刻的體會了 JSX 就是 JS 這個概念。

而在組件間數(shù)據(jù)傳遞部分,Props 是核心關(guān)鍵點。

但是,你可能還不知道,Props 有一個非常重要的特性:只讀,意思就是我們在組件中獲取到的 Props 數(shù)據(jù),你只能用,而不能改。

官方手冊的原話是:

  • 組件無論是使用函數(shù)聲明還是通過class 聲明,都絕不能修改自身的 Props……
  • React 非常靈活,但它也有一個嚴格的規(guī)則:所有 React 組件都必須像純函數(shù)一樣保護它們的 props 不被更改。

這里提到了一個 函數(shù)式編程 中的 純函數(shù) 概念,我簡單解釋一下什么是純函數(shù):“函數(shù)不會在函數(shù)體中修改入?yún)?,且在多次調(diào)用下,相同的入?yún)⑹冀K返回相同的結(jié)果。”

其實,從這里可以看出,React 團隊希望組件本身是具有純函數(shù)特性的,因此,Props 就需要設(shè)置為只讀的,不能在組件中被修改。

但是問題是,我們界面展示的內(nèi)容是需要根據(jù)數(shù)據(jù)改變而改變的,而 React 設(shè)計的核心理念中有非常重要的一點就是 數(shù)據(jù)驅(qū)動 UI。

看到這里,你一定會覺得 React 在自相矛盾,一邊傳入的 Props 數(shù)據(jù)是只讀不可改變的,一邊又依靠數(shù)據(jù)的改變而驅(qū)動 DOM 的改變。

真的是這樣嗎?

當然不是,數(shù)據(jù)驅(qū)動 UI 中的數(shù)據(jù)另有其人,它稱之為 “state”,也叫狀態(tài),作用就是在不違反上述規(guī)則的情況下,state 允許 React 組件隨用戶操作、網(wǎng)絡(luò)響應(yīng)或者其他變化而動態(tài)更改輸出的內(nèi)容。

 

責任編輯:姜華 來源: 勾勾的前端世界
相關(guān)推薦

2012-09-21 15:18:38

Java項目Java開發(fā)

2021-09-01 18:42:57

React Props組件

2021-08-24 05:07:25

React

2011-12-22 10:18:43

美信云網(wǎng)管

2021-08-26 00:33:29

React JSX語法

2021-08-26 18:46:55

React組件前端

2009-08-04 16:09:38

ASP.NET入門

2024-01-09 09:06:13

2020-08-26 07:48:41

React Spect組件庫開發(fā)

2009-07-24 14:15:51

數(shù)據(jù)訪問層

2010-09-13 16:13:47

DIV CSS表單

2010-09-02 14:44:41

DIV CSS表單

2011-04-01 10:11:25

OSPF路由器

2025-04-01 08:30:00

Plotly數(shù)據(jù)可視化數(shù)據(jù)分析

2018-04-04 15:52:54

銳捷

2024-09-12 14:51:27

2010-08-24 10:53:52

云計算基礎(chǔ)設(shè)施

2023-08-01 19:16:01

Spring編程瀏覽器

2014-01-16 16:53:53

storm事務(wù)一致性

2019-05-17 09:20:40

微軟機器學(xué)習(xí)小冰
點贊
收藏

51CTO技術(shù)棧公眾號