需求:
現(xiàn)在有兩個 component
,一個是 parent
,一個是 child
,在 parent component
中,我需要發(fā)送一個請求獲取到數(shù)據(jù),然后我需要把數(shù)據(jù)傳遞給 child component
, child component
在拿到數(shù)據(jù)后再去根據(jù)傳遞過來的數(shù)據(jù)發(fā)送請求。
嘗試的方法:
在 parent component
的 componentDidMount
方法中發(fā)送請求,然后在 render
中把請求的 props
數(shù)據(jù)傳遞給 child component
,child component
在 componentDidMount
中在去接收傳遞過來的數(shù)據(jù)進行請求數(shù)據(jù),結(jié)果發(fā)現(xiàn)并不能得到想要的結(jié)果。原因是:
當(dāng) parent
render
之后,接著 child
就會 render
,然后 child componentDidMount
就會被調(diào)用,最后才會調(diào)用 parent componentDidMount
,那么我在 parent
中拿到數(shù)據(jù)后 re-render
,在 child
不會再次調(diào)用 componentDidMount
了(componentDidMount
只會執(zhí)行一次)
請問有其它的方法可以實現(xiàn)這個需求嗎?
parent在componentWillMount的時候去請求數(shù)據(jù),可以把數(shù)據(jù)用this.data = xxx;或者this.setState({data:xxx})保存
parent在render的時候獲取設(shè)置的數(shù)據(jù),然后傳遞給child
child在componentWillMount獲取傳遞下來的數(shù)據(jù),然后去請求數(shù)據(jù),請求成功,可以把數(shù)據(jù)用this.data = xxx;或者this.setState({data:xxx})保存
child在render的時候渲染出來請求的數(shù)據(jù)就可以了。
或者直接在render時獲取數(shù)據(jù),請求數(shù)據(jù)一起進行,不用生命周期也可以做。不過如果是異步的話感覺上不太好。比如用axios,一般是在then(data=>{xxx})后才獲取得到數(shù)據(jù),而此操作是異步的
理一下
首先表明,為什么將請求放入componentDidMount?
什么情況下將請求放入componentDidMount?
正如你了解的,componentDidMount只走一下,這一次是你通過ReactDom.render,也就是Virtual Dom 落實到真實dom之后走的,適合什么樣的請求呢? 適合頁面初始化的請求,而且不依賴回調(diào)數(shù)據(jù),好處是什么呢,請求干凈利落,僅僅用來做界面的初始化動作,但更經(jīng)常放入componentWillMount
一旦涉及到,依賴回調(diào)數(shù)據(jù)(比如你這里parent Container請求到的數(shù)據(jù)傳遞過來的情況),那么你應(yīng)該將你的請求放入一個可以重復(fù)調(diào)用的聲明周期中,類似什么?
componentWillReceiveProps中,這個監(jiān)聽props改變,這里可以監(jiān)聽parentContainer傳入的props,然后這里可以根據(jù)傳入的props進行傳入請求。
謝邀!
首先React組件一般分為容器組件和展示組件,這兩者到底有什么區(qū)別呢?展示組件
通常被實現(xiàn)為無狀態(tài)功能組件
(stateless functional components),也就是說它們沒有內(nèi)部狀態(tài),只是負責(zé)把獲取到的數(shù)據(jù)渲染出來,因為它不知道獲取到的數(shù)據(jù)是什么樣的,不知道props和state的變化,它永遠不應(yīng)該改變props中的數(shù)據(jù)。實際上,任何的組件從父級得到的props都是不可變化的。
而容器組件
知道數(shù)據(jù),知道數(shù)據(jù)的形態(tài)以及數(shù)據(jù)從何而來,所以它們負責(zé)處理業(yè)務(wù)邏輯,它們接收信息并對其進行格式化,以便由展示組件簡單地使用。一般情況,“容器組件”總是作為可視化組件的父級組件出現(xiàn)!
當(dāng)然,凡事都沒有絕對性!您也可以在展示組件處理業(yè)務(wù)邏輯,相比之下展示組件負責(zé)渲染數(shù)據(jù)更合理些!好了,廢話不說了,就問題而言大致有兩種處理思路:
第一種:在child component
聲明周期函數(shù)componentWillReceiveProps
內(nèi)監(jiān)聽nextProps,執(zhí)行余下操作。
第二種:所有請求都在父組件完成,獲取到最終的數(shù)據(jù),然后通過props傳遞給子類。至于兩個請求依賴關(guān)系,es6之前
普通ajax,我們要層層嵌套回調(diào),es6
我們可以直接用Promise
,Promise優(yōu)點就是多重鏈?zhǔn)秸{(diào)用
,可以避免層層嵌套回調(diào)
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團,成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達內(nèi)教育集團成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
浪潮集團項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負責(zé)iOS教學(xué)及管理工作。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。