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

React 入門第四步:組件間的值傳遞 Props

開發(fā) 前端
我們學(xué)習(xí)了父級組件向不同的子級組件傳遞數(shù)據(jù),以及子級組件如何接受數(shù)據(jù)并處理,而如果父級組件傳遞較為復(fù)雜的數(shù)據(jù)時,如何傳遞數(shù)據(jù),如何在子組件中使用,就需要我們進一步學(xué)習(xí)了解。

[[420985]]

本文轉(zhuǎn)載自微信公眾號「勾勾的前端世界」,作者西嶺。轉(zhuǎn)載本文請聯(lián)系勾勾的前端世界公眾號。

父組件向子組件傳值 -普通傳值

父級組件傳遞數(shù)據(jù)

默認情況由父級組件傳遞數(shù)據(jù)到子級組件,我們將需要傳遞的數(shù)據(jù),以屬性的方式,寫入組件中,如下:

  1. import React from'react' 
  2. // 引入單文件組件 
  3. import PropsClass from'./PropsClass' 
  4. import PropsFun from'./PropsFun' 
  5.  
  6. // 要傳遞的數(shù)據(jù) 
  7. const toData = [ 
  8.   {id:1,name:"劉能",age:66}, 
  9.   {id:2,name:"廣坤",age:16} 
  10.  
  11. functionApp() { 
  12.   return ( 
  13.     <div> 
  14.       {/* 將需要傳遞的數(shù)據(jù),以屬性的方式,寫入組件 */} 
  15.       <PropsClasstoClass={toData}/> 
  16.       <PropsFuntoFun={toData}/> 
  17.     </div> 
  18.   ) 
  19.  
  20. exportdefault App 

這樣就完成了父級組件向子級組件傳遞數(shù)據(jù)的任務(wù)。

那么組件又分為函數(shù)組件和類組件。下面,我們分別展示類組件和函數(shù)組件是如何獲取傳遞進來的數(shù)據(jù)的。

我們先看類組件的獲取方式。

class 子級組件接受數(shù)據(jù)

class 組件中使用 this.props.xx 屬性名獲取父級組件傳遞的數(shù)據(jù):

  1. import React, { Component, Fragment } from'react' 
  2.  
  3. exportclass PropsClass extends Component { 
  4.   render() { 
  5.     return ( 
  6.       <Fragment> 
  7.         <h1>接受Props 數(shù)據(jù)</h1> 
  8.         {console.log(this.props.toClass)}{/* 打印數(shù)據(jù) */} 
  9.         {/* 遍歷數(shù)據(jù) */} 
  10.         {this.props.toClass.map(item => 
  11.         ( 
  12.           <divkey={item.id}> 
  13.             <span>{item.name}</span><span>{item.age}</span> 
  14.           </div> 
  15.         ) 
  16.         )} 
  17.       </Fragment> 
  18.     ) 
  19.   } 
  20.  
  21. exportdefault PropsClass 

類組件中 this 操作相對容易,因此,React 默認會將父級組件的傳入的數(shù)據(jù)放入 props 屬性中。而在類組件中,如代碼所示,我們就可以直接使用 this.props 來獲取數(shù)據(jù)了。

函數(shù)子級組件接受數(shù)據(jù)

函數(shù)組件中,Props 數(shù)據(jù)會默認傳入函數(shù),因此需要在函數(shù)形參中獲取,直接使用即可。

  1. import React, { Fragment } from'react' 
  2.  
  3. // 函數(shù)形參獲取Props 傳值 
  4. functionPropsFun(props) { 
  5.   return ( 
  6.     <Fragment> 
  7.       <h1>函數(shù)接受Props </h1> 
  8.       {console.log(props.toFun)} 
  9.       {/* 遍歷數(shù)據(jù) */} 
  10.       {props.toFun.map(item=> 
  11.         ( 
  12.         <divkey={item.id}> 
  13.           <span>{item.name}</span> 
  14.         </div> 
  15.         ) 
  16.       )} 
  17.     </Fragment> 
  18.   ) 
  19.  
  20. exportdefault PropsFun 

前面我們學(xué)習(xí)了父級組件向不同的子級組件傳遞數(shù)據(jù),以及子級組件如何接受數(shù)據(jù)并處理,而如果父級組件傳遞較為復(fù)雜的數(shù)據(jù)時,如何傳遞數(shù)據(jù),如何在子組件中使用,就需要我們進一步學(xué)習(xí)了解。

父組件向子組件傳值 -解構(gòu)傳值

父級組件傳遞數(shù)據(jù)

傳遞普通數(shù)據(jù),前面我們已經(jīng)接觸過了,如果要是傳遞的數(shù)據(jù)是數(shù)組或者對象,我們應(yīng)該如何處理呢?

最直接的辦法就是在傳遞時,在父級組件中將數(shù)據(jù)先進行解構(gòu),因為解構(gòu)出來的數(shù)據(jù),正好就是符合組件 “屬性” 寫法的:

  1. import React from'react' 
  2. // 引入單文件組件 
  3. import PropsClass from'./PropsClass' 
  4. import PropsFun from'./PropsFun' 
  5.  
  6. // 要傳遞的數(shù)據(jù) 
  7. const toData = [ 
  8.   {id:1,name:"劉能",age:66}, 
  9.   {id:2,name:"廣坤",age:16} 
  10.  
  11. functionApp() { 
  12.   return ( 
  13.     <div> 
  14.       {/* 結(jié)構(gòu)數(shù)據(jù)并傳入*/} 
  15.       <PropsClass{...toData[0]}/> 
  16.       <PropsFun{...toData[1]}/> 
  17.     </div> 
  18.   ) 
  19.  
  20. exportdefault App 

上面是解構(gòu)傳參。而在子級組件中應(yīng)用時,與普通的應(yīng)用并沒有區(qū)別,按照解構(gòu)好的對應(yīng)格式進行接收就可以了。

下面我們分別展示類組件和函數(shù)組件中獲取解構(gòu)傳參的方式。

class 子級組件接受數(shù)據(jù)

依然使用 props 獲取傳參。

  1. import React, { Component, Fragment } from'react' 
  2.  
  3. exportclass PropsClass extends Component { 
  4.  
  5.   render() { 
  6.     // 獲取傳入的解構(gòu)數(shù)據(jù) 
  7.     const {name,age} =this.props 
  8.     return ( 
  9.       <Fragment> 
  10.         <h1>Class 接受Props 數(shù)據(jù)</h1> 
  11.         {console.log(name,age,'--')}{/* 打印數(shù)據(jù) */} 
  12.  
  13.       </Fragment> 
  14.     ) 
  15.   } 
  16.  
  17. exportdefault PropsClass 

函數(shù)子級組件接受數(shù)據(jù)

依然使用函數(shù)形參獲取數(shù)據(jù)。

  1. import React, { Fragment } from'react' 
  2.  
  3. // 函數(shù)形參獲取Props 傳值 (結(jié)構(gòu)) 
  4. functionPropsFun({ name, age }) { 
  5.   return ( 
  6.     <Fragment> 
  7.       <h1>函數(shù)接受Props </h1> 
  8.       fun 數(shù)據(jù): 
  9.       {console.log(age, name)} 
  10.       <div> 
  11.         <span>{name}</span> 
  12.         <span>{age}</span> 
  13.       </div> 
  14.     </Fragment> 
  15.   ) 
  16.  
  17. exportdefault PropsFun 

設(shè)置默認值

在一定的條件下,父級組件即便沒有傳入數(shù)據(jù),子組件依然需要展示相關(guān)內(nèi)容。那么此時,我們就可以在子組件中設(shè)置默認值來填充,當父級組件沒有傳入數(shù)據(jù)時,子組件使用默認數(shù)據(jù),而如果父級組件有數(shù)據(jù)傳入,則替換默認值。

父級組件可以傳入數(shù)據(jù),也可以不傳入:

  1. import React from'react' 
  2. // 引入單文件組件 
  3. import PropsClass from'./PropsClass' 
  4. import PropsFun from'./PropsFun' 
  5.  
  6. functionApp() { 
  7.   return ( 
  8.     <div> 
  9.       {/* 父級組件沒有傳值則使用子組件的默認值,傳遞則替換 */} 
  10.       <PropsClassnames="llll"/> 
  11.       <PropsFun/> 
  12.     </div> 
  13.   ) 
  14.  
  15. exportdefault App 

類組件設(shè)置默認值

class 子組件中使用 static defaultProps 設(shè)置默認值,當然,我們依然需要從 this.props 中獲取。

  1. import React, { Component, Fragment } from'react' 
  2.  
  3. exportclass PropsClass extends Component { 
  4.  
  5.   // 此時我們就設(shè)置了 props 的默認值, 
  6.   // 如果父組件沒有傳遞數(shù)據(jù),則默認使用 
  7.   // 如果傳遞了數(shù)據(jù),則替換默認值 
  8.   static defaultProps = { 
  9.     names:'西嶺老濕'
  10.     age:18 
  11.   } 
  12.  
  13.  
  14.   render() { 
  15.     // 獲取組件傳入的數(shù)據(jù),可能是默認值,也可能是傳入的數(shù)據(jù) 
  16.     const {names,age} =this.props 
  17.     return ( 
  18.       <Fragment> 
  19.         <h2>Class 組件</h2> 
  20.         <p>{names}</p> 
  21.         <p>{age}</p> 
  22.       </Fragment> 
  23.     ) 
  24.   } 
  25.  
  26. exportdefault PropsClass 

函數(shù)組件設(shè)置默認值

函數(shù)組件需要使用組件名 .defaultProps 設(shè)置一個對象作為默認值,依然使用形參獲取:

  1. import React, { Fragment } from'react' 
  2.  
  3. // 函數(shù)形參獲取Props 傳值 (結(jié)構(gòu)) 
  4. functionPropsFun({ name, age }) { 
  5.   return ( 
  6.    <div> 
  7.      <h2>函數(shù)組件</h2> 
  8.      <p>{name}</p> 
  9.      <p>{age}</p> 
  10.    </div> 
  11.   ) 
  12.  
  13. // 函數(shù)組件需要使用組件名.defaultProps設(shè)置一個對象 
  14. PropsFun.defaultProps= { 
  15.   name:'西嶺'
  16.   age:16 
  17.  
  18. exportdefault PropsFun 

如果不想在子組件的形參接收時解構(gòu),也可以直接獲取 props。

  1. import React, { Fragment } from'react' 
  2.  
  3. // 函數(shù)形參獲取Props 傳值 (結(jié)構(gòu)) 
  4. functionPropsFun(props) { 
  5.   return ( 
  6.    <div> 
  7.      <h2>函數(shù)組件</h2> 
  8.      <p>{props.name}</p> 
  9.      <p>{props.age}</p> 
  10.    </div> 
  11.   ) 
  12.  
  13. // 函數(shù)組件需要使用組件名.defaultProps設(shè)置一個對象 
  14. PropsFun.defaultProps= { 
  15.   name:'西嶺'
  16.   age:16 
  17.  
  18. exportdefault PropsFun 

向子組件傳遞 JSX

父級組件傳遞 JSX

在父級組件中,需要向子級組件傳遞 JSX ,需要將 jsx 寫在組件的雙標簽內(nèi)。

  1. import React from'react' 
  2. // 引入單文件組件 
  3. import PropsClass from'./PropsClass' 
  4. import PropsFun from'./PropsFun' 
  5.  
  6. functionApp() { 
  7.   return ( 
  8.     <div> 
  9.       <h1>我是App</h1> 
  10.       {/* 需要傳遞 JSX ,寫在組件雙標簽內(nèi)*/} 
  11.       <PropsClass> 
  12.         {/* 可以傳遞多個標簽*/} 
  13.         <p>父級組件中傳入的JSX, p標簽,App-Class組件</p> 
  14.         <span>父級組件中傳入的JSX,span標簽,App-Class組件</span> 
  15.       </PropsClass> 
  16.       <PropsFun/> 
  17.     </div> 
  18.   ) 
  19.  
  20. exportdefault App 

class 子組件接收 JSX

使用 this.props.children 可以接收父級組件中傳入的全部 JSX。

  1. import React, { Component, Fragment } from'react' 
  2. exportclass PropsClass extends Component { 
  3.   render() { 
  4.  
  5.     return ( 
  6.       <Fragment> 
  7.         <h2>Class 組件</h2> 
  8.         {/* 接收 JSX ,可以接收多個*/} 
  9.         {this.props.children} 
  10.       </Fragment> 
  11.     ) 
  12.   } 
  13.  
  14. exportdefault PropsClass 

函數(shù)子組件接收 JSX

函數(shù)組件中獲取 jsx ,可以直接使用 props 接收參數(shù)。

  1. import React, { Fragment } from'react' 
  2.  
  3. // 函數(shù)組件中獲取jsx ,可以直接使用 props 接收參數(shù) 
  4. functionPropsFun(props) { 
  5.   return ( 
  6.    <div> 
  7.      <h2>函數(shù)組件</h2> 
  8.      <p>{props.name}</p> 
  9.      <p>{props.age}</p> 
  10.      {props.children} 
  11.    </div> 
  12.   ) 
  13.  
  14. // 函數(shù)組件需要使用組件名.defaultProps設(shè)置一個對象 
  15. PropsFun.defaultProps= { 
  16.   name:'西嶺'
  17.   age:16 
  18.  
  19. exportdefault PropsFun 

 

責(zé)任編輯:武曉燕 來源: 勾勾的前端世界
相關(guān)推薦

2012-09-21 15:13:10

Java項目Java開發(fā)

2011-12-14 14:03:44

美信云網(wǎng)管告警

2021-09-02 18:46:40

React CSS 組件

2021-08-26 18:46:55

React組件前端

2021-08-24 05:07:25

React

2021-08-26 00:33:29

React JSX語法

2009-10-22 19:04:53

shell命令

2021-07-26 09:35:26

SQL數(shù)據(jù)庫優(yōu)化

2010-04-28 12:02:37

Forefront網(wǎng)絡(luò)優(yōu)化

2021-11-23 23:43:16

MySQL數(shù)據(jù)庫Docker

2010-06-12 13:49:16

學(xué)習(xí)UML

2010-06-13 14:19:40

學(xué)習(xí)UML

2010-09-06 11:58:39

ppp撥號Linux

2010-09-14 17:35:52

2017-04-17 12:31:45

SDN網(wǎng)絡(luò)虛擬化

2010-04-20 10:12:05

2010-06-02 17:29:02

svnserve服務(wù)

2010-11-19 15:44:04

IT跳槽

2011-07-07 13:09:04

編程

2013-07-15 14:30:44

產(chǎn)品經(jīng)理
點贊
收藏

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