網(wǎng)站首頁 編程語言 正文
父組件向子組件通信
在父組件中,為子組件添加屬性數(shù)據(jù),即可實(shí)現(xiàn)父組件向子組件通信。傳遞的數(shù)據(jù)可以分成兩類
子組件是作為屬性來接收這些數(shù)據(jù)的
第一類就是數(shù)據(jù):變量,對(duì)象,屬性數(shù)據(jù),狀態(tài)數(shù)據(jù)等等
這些數(shù)據(jù)發(fā)生改變,子組件接收的屬性數(shù)據(jù)就發(fā)生了改變。
第二類就是方法:父組件可以向子組件傳遞屬性方法,子組件接收方法,并可以在組件內(nèi)執(zhí)行,有兩種執(zhí)行方式
注意:父組件傳給子組件的方法是不能執(zhí)行的,執(zhí)行了相當(dāng)于將方法的返回值傳遞給子組件。
第一種 作為事件回調(diào)函數(shù)執(zhí)行
參數(shù)默認(rèn)就是事件對(duì)象
this默認(rèn)指向undefined,因此我們要改變this指向
如果在子組件中改變,
this指向子組件
bind方式傳遞的參數(shù)在事件對(duì)象之前
如果在父組件中改變(??工作中常用?
?)
this指向父組件了
此時(shí)子組件不論如何綁定,都指向父組件
bind方式傳遞的參數(shù)會(huì)在子組件傳遞的參數(shù)之前
參數(shù)順序:父組件參數(shù),子組件參數(shù),事件對(duì)象
第二種 在子組件方法中執(zhí)行
默認(rèn)沒有參數(shù),使用什么可以傳遞什么
this默認(rèn)指向子組件屬性對(duì)象(this.props)
此時(shí)我們要在父組件中綁定this,就指向了父組件
我們既可以訪問父組件實(shí)例化對(duì)象,也可以訪問子組件實(shí)例化對(duì)象,
我們既可以在子組件中傳遞參數(shù),也可以在父組件中傳遞參數(shù)
有一種簡(jiǎn)寫方式(??工作中常用?
?)
直接在事件回調(diào)函數(shù)中,定義箭頭函數(shù),并執(zhí)行父組件方法:
// 定義父組件slider class Slider extends Component { // 構(gòu)造函數(shù) constructor(props) { super(props); // 初始化狀態(tài) this.state = { title: '返回頂部' } } // 父組件方法 parentMethod() { console.log(this, arguments) } render() { return ( <div className="slider"> {/*字符串*/} {/*<GoTop text="網(wǎng)址導(dǎo)航"></GoTop>*/} {/*屬性數(shù)據(jù)*/} {/*<GoTop text={this.props.title}></GoTop>*/} {/*狀態(tài)數(shù)據(jù)*/} {/*<GoTop text={this.state.title}></GoTop>*/} {/*傳遞方法*/} <GoTop text={this.state.title} method={this.parentMethod.bind(this, 200, 'parent')}></GoTop> {/*<GoTop text={this.state.title} method={this.parentMethod}></GoTop>*/} </div> ) } } // 定義組件 class GoTop extends Component { // 定義方法 childMethod(e) { // 執(zhí)行父組件方法 this.props.method(200, e); } // 4 渲染虛擬DOM render() { // console.log(this.props) // 綁定事件 // return <span onClick={this.props.method.bind(this, 100, 'child')}>{this.props.text}</span> // 在子組件方法中執(zhí)行 // return <span onClick={this.childMethod.bind(this)}>{this.props.text}</span> // 簡(jiǎn)便寫法 return <span onClick={e => this.props.method(e)}>{this.props.text}</span> } }
存在期
組件創(chuàng)建完成,一旦屬性數(shù)據(jù)或者狀態(tài)數(shù)據(jù)發(fā)生改變,組件就會(huì)進(jìn)入存在期,共分五個(gè)階段
第一個(gè)階段 組件即將接收新的屬性數(shù)據(jù)
componnetWillReceiveProps方法
第一個(gè)參數(shù)表示??新的?
?屬性數(shù)據(jù)
組件實(shí)例化對(duì)象上的是舊的屬性數(shù)據(jù)
數(shù)據(jù)還沒有更新,
??只有當(dāng)屬性數(shù)據(jù)發(fā)生改變,才會(huì)執(zhí)行該方法,狀態(tài)數(shù)據(jù)改變不會(huì)執(zhí)行?
?,直接進(jìn)入第二個(gè)階段
作用:用屬性數(shù)據(jù)去更新狀態(tài)數(shù)據(jù),實(shí)現(xiàn)數(shù)據(jù)由外部流入內(nèi)部
第二個(gè)階段 組件是否應(yīng)該更新
shouldComponentUpdate方法
第一個(gè)參數(shù)表示??新的?
?屬性數(shù)據(jù)
第二個(gè)參數(shù)表示??新的?
?狀態(tài)數(shù)據(jù)
組件實(shí)例化對(duì)象上的是舊的屬性數(shù)據(jù)
組件實(shí)例化對(duì)象上的是舊的狀態(tài)數(shù)據(jù)
必須有返回值,表示是否可以更新
true 可以更新
false 不能更新
工作中,我們常常比較
參數(shù)中的屬性數(shù)據(jù)是否與組件實(shí)例化對(duì)象中屬性數(shù)據(jù)是否不相等,或者
參數(shù)中的狀態(tài)數(shù)據(jù)是否與組件實(shí)例化對(duì)象中狀態(tài)數(shù)據(jù)是否不相等
作用:啟動(dòng)更新優(yōu)化的作用,常常在高頻事件中使用。(類似節(jié)流作用)
第三個(gè)階段 組件即將更新
componentWillUpdate方法:
- 第一個(gè)參數(shù)表示?
?新的?
?屬性數(shù)據(jù) - 第二個(gè)參數(shù)表示?
?新的?
?狀態(tài)數(shù)據(jù)
組件實(shí)例化對(duì)象上的是舊的屬性數(shù)據(jù)
組件實(shí)例化對(duì)象上的是舊的狀態(tài)數(shù)據(jù)
說明此時(shí)數(shù)據(jù)仍然沒有更新,當(dāng)該方法執(zhí)行完畢,數(shù)據(jù)才會(huì)更新
作用:更新插件,預(yù)處理數(shù)據(jù)等等,
注意:不要在第二個(gè)階段和第三個(gè)階段去更新屬性數(shù)據(jù)以及裝填數(shù)據(jù)
第四個(gè)階段 組件渲染視圖
render 方法
沒有參數(shù),但是此時(shí)數(shù)據(jù)已經(jīng)更新了
- 組件實(shí)例化對(duì)象上的是?
?新的?
?屬性數(shù)據(jù) - 組件實(shí)例化對(duì)象上的是?
?新的?
?狀態(tài)數(shù)據(jù)
所以我們?cè)阡秩咎摂MDOM的時(shí)候,可以使用這些新的數(shù)據(jù)了
此時(shí)虛擬DOM還沒有更新,方法執(zhí)行完畢,虛擬DOM才更新
第五個(gè)階段 組件更新完成
componentDidUpdate方法:
- 第一個(gè)參數(shù)是舊的屬性數(shù)據(jù)
- 第二個(gè)參數(shù)是舊的狀態(tài)數(shù)據(jù)
組件實(shí)例化對(duì)象上的是??新的?
?屬性數(shù)據(jù)
組件實(shí)例化對(duì)象上的是??新的?
?狀態(tài)數(shù)據(jù)
此時(shí)虛擬DOM也已經(jīng)更新完成了
組件更新完成了,我們可以在這個(gè)階段發(fā)送請(qǐng)求,處理事件等等,該方法執(zhí)行完畢,并沒有說明存在期的結(jié)束,存在期仍然繼續(xù),只是一次更新的結(jié)束,所有組件生命周期方法this都指向組件實(shí)例化對(duì)象
// 定義組件 class GoTop extends Component { // 構(gòu)造函數(shù) constructor(props) { super(props); this.state = { text: props.text } } // 存在期五個(gè)階段 // 1 組件即將接收新的屬性數(shù)據(jù) componentWillReceiveProps(newProps) { console.log(111, 'componentWillRecieveProps', newProps, this.props) // 將屬性數(shù)據(jù),存儲(chǔ)到狀態(tài)中 this.setState({ text: newProps.text }) } // 2 組件是否更新 shouldComponentUpdate(newProps, newState) { console.log(222, 'shouldComponentUpdate', newProps, this.props, 'state', newState, this.state) // 是否可以更新 // return true; // 根據(jù)屬性或者狀態(tài)的改變來優(yōu)化 return newProps.text !== this.props.text || newState.text !== this.state.text } // 3 組件即將更新 componentWillUpdate(newProps, newState) { console.log(333, 'componentWillUpdate', newProps, this.props, 'state', newState, this.state, findDOMNode(this).innerHTML) } // 4 渲染虛擬DOM render() { console.log(444, 'render', this.props, 'state', this.state) // return <span>{this.props.text}</span> return <span>{this.state.text}</span> } // 5 組件更新完成 componentDidUpdate(oldProps, oldState) { console.log(555, 'componentDidUpdate', oldProps, this.props, 'state', oldState, this.state, findDOMNode(this).innerHTML) } // 組件創(chuàng)建完成 componentDidMount() { window.onscroll = () => { // 移動(dòng)超過300顯示返回頂部 if (window.scrollY > 300) { this.setState({ text: '返回頂部' }) } else { // 顯示頭條新聞 this.setState({ text: '頭條新聞' }) } // console.log(window.scrollY) } } }
原文鏈接:https://blog.51cto.com/u_13349380/5614763
相關(guān)推薦
- 2022-07-20 Python3.7.2環(huán)境安裝
- 2023-01-05 python?如何去除字符串中指定字符_python
- 2023-03-04 React深入分析更新的創(chuàng)建源碼_React
- 2022-04-23 通過CSS的sticky屬性 重新回顧 position
- 2022-02-03 ionic 富文本編輯樣式后,前臺(tái)不能回顯樣式
- 2022-04-27 bash?shell獲取當(dāng)前腳本的絕對(duì)路徑(pwd/readlink)_linux shell
- 2023-06-21 Android崩潰日志收集和保存解析_Android
- 2023-01-14 GoLang日志監(jiān)控系統(tǒng)實(shí)現(xiàn)_Golang
- 最近更新
-
- 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)證過濾器
- Spring Security概述快速入門
- 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)程分支