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

React 數(shù)據(jù)流管理:組件之間數(shù)據(jù)是如何傳遞的?

開發(fā) 前端
截至到目前,我們已經(jīng)掌握了組件的概念、組件的傳值、以及組件狀態(tài)的相關(guān)內(nèi)容。有興趣的寶子可以翻看俺之前發(fā)布的內(nèi)容,筆芯。

[[422977]]

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

截至到目前,我們已經(jīng)掌握了組件的概念、組件的傳值、以及組件狀態(tài)的相關(guān)內(nèi)容。有興趣的寶子可以翻看俺之前發(fā)布的內(nèi)容,筆芯。

你應(yīng)該發(fā)現(xiàn)了,我們學(xué)習(xí) React ,除了環(huán)境搭建和不多的 JSX 語(yǔ)法外,極大的篇幅都在學(xué)習(xí) React 中的數(shù)據(jù)流管理。

但是,我們?cè)谇懊鎸W(xué)習(xí)組件傳值時(shí),一直都是將值從父級(jí)組件傳入子級(jí)組件中,從來(lái)沒(méi)有將子級(jí)組件的值傳入到父級(jí)組件,也沒(méi)有在多個(gè)兄弟組件間傳值。

而根據(jù)單向數(shù)據(jù)流的內(nèi)容推測(cè),我們也不敢確定數(shù)據(jù)是否可以從子組件傳入父級(jí)組件。

為什么沒(méi)有在之前就說(shuō)呢?因?yàn)槲覀兊幕A(chǔ)知識(shí)掌握的還不夠,現(xiàn)在學(xué)完組件狀態(tài),就可以了,那到底應(yīng)該怎么做呢?

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

子組件向父組件傳值

我們先來(lái)看子級(jí)組件如何向父級(jí)組件傳遞數(shù)據(jù):其本質(zhì)上就是使用回調(diào)函數(shù)。

具體怎么做呢?

父級(jí)組件引入子級(jí)組件后,在 JSX 中依然使用 Props 的方式,將提前寫好的父級(jí)組件的函數(shù),傳入子級(jí)組件,在子級(jí)組件中使用 this.props 接收傳入的函數(shù)并調(diào)用,在函數(shù)的調(diào)用中,將需要出入父級(jí)組件的值,傳入函數(shù)調(diào)用的實(shí)參。

父組件:

  1. import React, { Component } from 'react' 
  2. import States from './States' 
  3.  
  4. export class App extends Component { 
  5.   state = { 
  6.     name:'lisi' 
  7.   } 
  8.  
  9.   callBack = (req)=>{ 
  10.     this.setState({name:req}) 
  11.   } 
  12.  
  13.   render() { 
  14.     return ( 
  15.       <div> 
  16.         <h1>APP</h1> 
  17.         <p>{this.state.name}</p> 
  18.         {/* 將父級(jí)組件中的函數(shù),以 Props 的方式傳入子級(jí)組件 */} 
  19.         {/* 子級(jí)組件調(diào)用函數(shù),以回調(diào)函數(shù)的方式將子組件的值傳回給父級(jí)組件 */} 
  20.         <States fun={this.callBack} /> 
  21.       </div> 
  22.     ) 
  23.   } 
  24.  
  25. export default App 

子組件:

  1. import React, { Component } from 'react' 
  2.  
  3. export class States extends Component { 
  4.   render() { 
  5.     return ( 
  6.       <div> 
  7.         {/* 子組件使用 Props 接收父級(jí)組件傳入的函數(shù)并調(diào)用 */} 
  8.         {/* 將需要傳入父級(jí)組件的值,以實(shí)參的方式傳入到函數(shù)調(diào)用 */} 
  9.         <button onClick={()=>{this.props.fun('xliling')}}>點(diǎn)我</button> 
  10.       </div> 
  11.     ) 
  12.   } 
  13.  
  14. export default States 

父級(jí)組件向子級(jí)組件通信,我們使用的是 Props 屬性,子級(jí)組件向父級(jí)組件通信則是結(jié)合了 Props 和回調(diào)函數(shù)進(jìn)行實(shí)現(xiàn)的,集合這兩點(diǎn),我們就可以實(shí)現(xiàn)兄弟組件的通信了。

兄弟組件通信

兄弟組件的通信原理其實(shí)也很簡(jiǎn)單,就是使用回調(diào)函數(shù)的方式,先將數(shù)據(jù)傳入父級(jí)組件,再由父級(jí)組件使用 Props 的方式將數(shù)據(jù)傳入子級(jí)組件,如下圖所示:

而具體代碼的實(shí)現(xiàn),并沒(méi)有什么新的知識(shí)點(diǎn)內(nèi)容,無(wú)非就是兩者結(jié)合一下而已:

我們?cè)诟讣?jí)組件中,引入子級(jí)組件的內(nèi)容,然后將函數(shù)傳入數(shù)據(jù)來(lái)源的子級(jí)組件,同樣使用 Props 再將數(shù)據(jù)傳入另一個(gè)組件中。

父級(jí)組件的代碼如下:

  1. import React, { Component } from 'react' 
  2.  
  3. import States from './States' 
  4. import Brother from './Brother' 
  5.  
  6. export class App extends Component { 
  7.   state = { 
  8.     name:'lisi' 
  9.   } 
  10.  
  11.   callBack = (req)=>{ 
  12.     this.setState({name:req}) 
  13.   } 
  14.  
  15.   render() { 
  16.     return ( 
  17.       <div> 
  18.         <h1>APP</h1> 
  19.         <p>{this.state.name}</p> 
  20.         <States fun={this.callBack} /> 
  21.         <Brother fromApp={this.state.name}></Brother> 
  22.       </div> 
  23.     ) 
  24.   } 
  25.  
  26. export default App 

接著我們看數(shù)據(jù)來(lái)源組件中,通過(guò) Props 獲取回調(diào)函數(shù),調(diào)用并傳入數(shù)據(jù):

  1. import React, { Component } from 'react' 
  2.  
  3. export class States extends Component { 
  4.   render() { 
  5.     return ( 
  6.       <div> 
  7.         <button onClick={()=>{this.props.fun('xliling')}}>點(diǎn)我</button> 
  8.       </div> 
  9.     ) 
  10.   } 
  11.  
  12. export default States 

然后再接收數(shù)據(jù)的子組件中,獲取數(shù)據(jù):

  1. import React, { Component } from 'react' 
  2.  
  3. export class Brother extends Component { 
  4.   render() { 
  5.     return ( 
  6.       <div> 
  7.         <h2>Brother </h2> 
  8.         <p>{this.props.fromApp}</p> 
  9.       </div> 
  10.     ) 
  11.   } 
  12.  
  13. export default Brother 

總結(jié)

子組件向父級(jí)組件傳值就是簡(jiǎn)單的回調(diào)函數(shù),并沒(méi)有復(fù)雜的手法。,而利用回調(diào)函數(shù)和 Props 也可以輕松的實(shí)現(xiàn)兄弟組件間的數(shù)據(jù)傳遞,至此,我們利用 Props 完成了對(duì) React 數(shù)據(jù)流管理的所有內(nèi)容的學(xué)習(xí)。

 

而之前提到的關(guān)于 JSX 交互部分,用戶的頁(yè)面操作,都是由表單承接的。那么接下來(lái)的表單的處理就是重點(diǎn)了,下周再更,嘻嘻(●'?'●)

 

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

2013-12-11 09:47:35

NetflixAWSIaaS

2017-07-11 18:00:21

vue.js數(shù)據(jù)組件

2013-06-19 09:49:34

云計(jì)算大數(shù)據(jù)Hadoop

2021-03-05 15:55:10

鴻蒙HarmonyOS應(yīng)用開發(fā)

2017-12-12 10:50:54

數(shù)據(jù)中心機(jī)柜氣流

2011-07-13 09:31:48

ASP.NET數(shù)據(jù)傳遞

2011-07-28 14:49:40

2023-07-28 13:55:40

便捷選項(xiàng)組件

2009-12-16 09:16:53

ASP.NET頁(yè)面間數(shù)

2017-11-01 14:29:38

2021-07-01 10:13:51

緩存數(shù)據(jù)存儲(chǔ)服務(wù)化架構(gòu)

2021-09-07 18:40:55

單向數(shù)據(jù)流數(shù)據(jù)

2019-12-19 14:38:08

Flink SQL數(shù)據(jù)流Join

2022-11-14 09:13:16

2016-09-14 21:44:50

JavascriptreactJsjsx

2016-11-25 13:50:15

React組件SFC

2022-05-13 08:48:50

React組件TypeScrip

2023-12-21 10:26:30

??Prettier

2011-12-14 15:57:13

javanio

2021-07-05 11:06:11

組件React通信
點(diǎn)贊
收藏

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