網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
props理解
props 是 React 組件通信最重要的手段
props:對(duì)于在 React 應(yīng)用中寫(xiě)的子組件,父組件綁定在它們標(biāo)簽里的?屬性和方法,最終會(huì)變成 props 傳遞給它們。
1)props 可以是:
- ① props 作為一個(gè)子組件渲染數(shù)據(jù)源。
- ② props 作為一個(gè)通知父組件的回調(diào)函數(shù)。
- ③ props 作為一個(gè)單純的組件傳遞。
- ④ props 作為渲染函數(shù)。
- ⑤ render props , 和④的區(qū)別是放在了 children 屬性上。
- ⑥ render component 插槽組件。
/* children 組件 */ function ChidrenComponent(){ return <div> In this chapter, let's learn about react props ! </div> } /* props 接受處理 */ class PropsComponent extends React.Component{ componentDidMount(){ console.log(this,'_this') } render(){ const { children , mes , renderName , say ,Component } = this.props const renderFunction = children[0] const renderComponent = children[1] /* 對(duì)于子組件,不同的props是怎么被處理 */ return <div> { renderFunction() } { mes } { renderName() } { renderComponent } <Component /> <button onClick={ () => say() } > change content </button> </div> } } /* props 定義綁定 */ class Index extends React.Component{ state={ mes: "hello,React" } node = null say= () => this.setState({ mes:'let us learn React!' }) render(){ return <div> <PropsComponent mes={this.state.mes} // ① props 作為一個(gè)渲染數(shù)據(jù)源 say={ this.say } // ② props 作為一個(gè)回調(diào)函數(shù) callback Component={ ChidrenComponent } // ③ props 作為一個(gè)組件 renderName={ ()=><div> my name is alien </div> } // ④ props 作為渲染函數(shù) > { ()=> <div>hello,world</div> } { /* ⑤render props */ } <ChidrenComponent /> { /* ⑥r(nóng)ender component */ } </PropsComponent> </div> } }
2)props在React充當(dāng)角色(3個(gè)角度):
① 組件層級(jí):
- ??父?jìng)髯樱簆rops?和?子傳父:props 的 callback
- 將視圖容器作為 props 進(jìn)行渲染
② 更新機(jī)制
? 在 fiber 調(diào)和階段中,diff 可以說(shuō)是 React 更新的驅(qū)動(dòng)器,props 可以作為組件是否更新的重要準(zhǔn)則
? (PureComponent
,memo
等性能優(yōu)化方案)
③ 插槽層面
? 組件的閉合標(biāo)簽里的插槽,轉(zhuǎn)化成 chidren 屬性
3)監(jiān)聽(tīng)props改變:
類組件:?componentWillReceiveProps(廢棄) componentWillReceiveProps(新)函數(shù)組件:?useEffect (初始化會(huì)默認(rèn)執(zhí)行一次) props chidren模式
① props 插槽組件
<Container> <Children> </Container>
在 Container 組件中,通過(guò)?props.children?屬性訪問(wèn)到 Chidren 組件,為 React element 對(duì)象。
作用:
- 可以根據(jù)需要控制 Chidren 是否渲染。
- Container 可以用 React.cloneElement 強(qiáng)化 props (混入新的 props ),或者修改 Chidren 的子元素。
② render props模式
<Container> { (ContainerProps)=> <Children {...ContainerProps} /> } </Container> ———————————————————————————————————————————————————————————————————————————————— Container組件: function Container(props) { const ContainerProps = { name: 'alien', mes:'let us learn react' } return props.children(ContainerProps) }
根據(jù)需要控制 Chidren 渲染與否。可以將需要傳給 Children 的 props 直接通過(guò)函數(shù)參數(shù)的方式傳遞給執(zhí)行函數(shù) children 。
操作 props
1、抽象 props
用于跨層級(jí)傳遞 props ,一般不需要具體指出 props 中某個(gè)屬性,而是將 props 直接傳入或者是抽離到子組件中。
1)混入 props
給父組件 props 中混入某個(gè)屬性,再傳遞給子組件
function Son(props){ console.log(props) return <div> hello,world </div> } function Father(props){ const fatherProps={ mes:'let us learn React !' } return <Son {...props} { ...fatherProps } /> } function Index(){ const indexProps = { name:'alien', age:'28', } return <Father { ...indexProps } /> }
2)抽離 props
從父組件 props 中抽離某個(gè)屬性,再傳遞給子組件
function Son(props){ console.log(props) return <div> hello,world </div> } function Father(props){ const { age,...fatherProps } = props return <Son { ...fatherProps } /> } function Index(){ const indexProps = { age:'28', mes:'let us learn React !' } return <Father { ...indexProps } /> }
2、注入 props
1)顯式注入 props
能夠直觀看見(jiàn)標(biāo)簽中綁定的 props
function Son(props){ console.log(props) return <div> hello,world </div> } function Father(props){ const fatherProps={ mes:'let us learn React !' } return <Son {...props} { ...fatherProps } /> } function Index(){ const indexProps = { name:'alien', age:'28', } return <Father { ...indexProps } /> }
2)隱式注入 props
一般通過(guò) React.cloneElement 對(duì) props.chidren 克隆再混入新的 props
function Son(props){ console.log(props) // {name: "alien", age: "28", mes: "let us learn React !"} return <div> hello,world </div> } function Father(prop){ return React.cloneElement(prop.children,{ mes:'let us learn React !' }) } function Index(){ return <Father> <Son name="alien" age="28" /> </Father> }
總結(jié)
1、pros作用、角色
2、props的children(插槽)
3、操作props
原文鏈接:https://blog.csdn.net/weixin_45654582/article/details/122447850
相關(guān)推薦
- 2023-04-03 PyTorch加載模型model.load_state_dict()問(wèn)題及解決_python
- 2022-06-16 linux系統(tǒng)安裝hadoop真分布式集群詳解_云計(jì)算技術(shù)
- 2023-08-01 v-model 和 .sync 深度解讀
- 2022-01-29 win server 2008 web IIS部署asp.net程序后,CSS樣式錯(cuò)亂不顯示問(wèn)題
- 2022-04-30 DataGridView不顯示最下面的新行、判斷新增行、刪除行操作_C#教程
- 2022-05-05 Python學(xué)習(xí)之流程控制與條件判斷總結(jié)_python
- 2022-07-10 數(shù)組的遍歷方法有哪些
- 2022-07-02 Python零錢(qián)兌換的實(shí)現(xiàn)代碼_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支