React RenderProps模式運(yùn)用過(guò)程淺析
1.引入
上代碼:
import React, { Component } from 'react' import './index.css' export default class Parent extends Component { render() { return ( <div className="parent"> <h3>我是Parent組件</h3> <A/> </div> ) } } class A extends Component { render() { console.log(this.props); return ( <div className="a"> <h3>我是A組件</h3> </div> ) } }
結(jié)果很簡(jiǎn)單就能猜到
改一下呢?
import React, { Component } from 'react' import './index.css' export default class Parent extends Component { render() { return ( <div className="parent"> <h3>我是Parent組件</h3> <A>Hello !</A> </div> ) } } class A extends Component { render() { console.log(this.props); return ( <div className="a"> <h3>我是A組件</h3> </div> ) } }
頁(yè)面是沒(méi)有現(xiàn)實(shí)Hello !的,但是之前一次的封裝NaLink也有傳遞過(guò)標(biāo)簽體內(nèi)容的,在子組件的props中,children:(內(nèi)容)
所以A組件想要展示傳遞的標(biāo)簽體內(nèi)容的話,還要改一下A組件
class A extends Component { render() { console.log(this.props); return ( <div className="a"> <h3>我是A組件</h3> {this.props.children} </div> ) } }
2.改一下呢
import React, { Component } from 'react' import './index.css' export default class Parent extends Component { render() { return ( <div className="parent"> <h3>我是Parent組件</h3> <A> <B/> </A> </div> ) } } class A extends Component { state ={ name:'Mike'} render() { console.log(this.props); return ( <div className="a"> <h3>我是A組件</h3> {this.props.children} </div> ) } } class B extends Component { render() { console.log('B--render'); return ( <div className="b"> <h3>我是B組件</h3> </div> ) } }
A,B組件成了父子組件
但是這樣,如果A組件想傳自己的值給B組件,貌似是行不通的
3.再改一下呢
import React, { Component } from 'react' import './index.css' import C from '../1_setState' export default class Parent extends Component { render() { return ( <div className="parent"> <h3>我是Parent組件</h3> <A render={(name) => <B name={name}/>} /> </div> ) } } class A extends Component { state ={ name:'Mike'} render() { const {name} =this.state; console.log(this.props); return ( <div className="a"> <h3>我是A組件</h3> {this.props.render(name)} </div> ) } } class B extends Component { render() { console.log('B--render'); return ( <div className="b"> <h3>我是B組件,接收到的name:{this.props.name}</h3> </div> ) } }
主要是Parent組件和A組件之間調(diào)用要注意:
Parent組件中,render(當(dāng)然可以去其他的名字這里)這樣寫(xiě),相當(dāng)于預(yù)留了一個(gè)插槽,如果你需要渲染其他組件(例如例子中的B組件),在A組件中調(diào)用this.props.render()就可以渲染出B組件,不寫(xiě)的話就不會(huì)渲染出B組件
4.總結(jié)一下
如何向組件內(nèi)部動(dòng)態(tài)傳入帶內(nèi)容的結(jié)構(gòu)(標(biāo)簽)
Vue中:
使用slot技術(shù), 也就是通過(guò)組件標(biāo)簽體傳入結(jié)構(gòu)
React中:
使用children props: 通過(guò)組件標(biāo)簽體傳入結(jié)構(gòu)
使用render props: 通過(guò)組件標(biāo)簽屬性傳入結(jié)構(gòu), 一般用render函數(shù)屬性
children props
<A>
<B>xxxx</B>
</A>
{this.props.children}
問(wèn)題: 如果B組件需要A組件內(nèi)的數(shù)據(jù), ==> 做不到
render props
<A render={(data) => <C data={data}></C>}></A>
A組件: {this.props.render(內(nèi)部state數(shù)據(jù))}
C組件: 讀取A組件傳入的數(shù)據(jù)顯示 {this.props.data}
到此這篇關(guān)于React RenderProps模式運(yùn)用過(guò)程淺析的文章就介紹到這了,更多相關(guān)React RenderProps內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React實(shí)現(xiàn)導(dǎo)入導(dǎo)出Excel文件
本文主要介紹了React實(shí)現(xiàn)導(dǎo)入導(dǎo)出Excel文件,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07React路由中的redux和redux知識(shí)點(diǎn)拓展
這篇文章主要介紹了React路由中的redux和redux知識(shí)點(diǎn)拓展,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的朋友可以參考學(xué)習(xí)一下2022-08-08React中useTransition鉤子函數(shù)的使用詳解
React?18的推出標(biāo)志著React并發(fā)特性的正式到來(lái),其中useTransition鉤子函數(shù)是一個(gè)重要的新增功能,下面我們就來(lái)學(xué)習(xí)一下useTransition鉤子函數(shù)的具體使用吧2024-02-02ES6 class類鏈?zhǔn)嚼^承,實(shí)例化及react super(props)原理詳解
這篇文章主要介紹了ES6 class類鏈?zhǔn)嚼^承,實(shí)例化及react super(props)原理,結(jié)合實(shí)例形式詳細(xì)分析了ES6 中class類鏈?zhǔn)嚼^承,實(shí)例化及react super(props)原理相關(guān)概念、原理、定義與使用技巧,需要的朋友可以參考下2020-02-02React?Hooks?實(shí)現(xiàn)的中文輸入組件
這篇文章主要為大家介紹了React?Hooks實(shí)現(xiàn)的中文輸入組件示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05React中Refs的使用場(chǎng)景及核心要點(diǎn)詳解
在使用?React?進(jìn)行開(kāi)發(fā)過(guò)程中,或多或少使用過(guò)?Refs?進(jìn)行?DOM?操作,這篇文章主要介紹了?Refs?功能和使用場(chǎng)景以及注意事項(xiàng),希望對(duì)大家有所幫助2023-07-07解決React報(bào)錯(cuò)No duplicate props allowed
這篇文章主要為大家介紹了React報(bào)錯(cuò)No duplicate props allowed解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12