[問題] React初始化資料的寫法疑問

作者: aoksc (重出江湖)   2017-08-01 22:44:26
請問各位
我用React來寫一個典型的查詢頁面
主要分成兩塊
分別是search board也是parent component
在這裡輸入資料並用ajax查詢
查詢完的結果送到result panel也就是child component
如果是第一次載入頁面
我預設查詢是寫在componentDidMount()
並用this.setState設定
但我發現我這樣做他並不會傳給child component
即便我有設定state並把值pass給result panel
但result panel接到的資料筆數卻是0筆(我是傳array)
請問像這種情況
我如果任何資料都是從parent component傳給child component
包含連初始化的資料也是
那請問初始化的資料要寫在哪裡才會正確的傳給child component
並且正確的render?
還有我用chrome dev tool看
我發現好像是從child component開始render
再換parent component render
請問是因為這樣的關係
所以即便我在parent component的componentDidMount()設定完資料
因為child component已經初始化完所以不會重新render?
請問這個現象有什麼文章可以學習呢?
謝謝
作者: chatnoir (對不起)   2017-08-01 23:07:00
你是用props傳到child嗎?
作者: aoksc (重出江湖)   2017-08-02 08:41:00
我是用state傳值
作者: illl (ill!)   2017-08-02 09:51:00
code貼上來阿
作者: spjay1 (Josh)   2017-08-02 22:54:00
child 有沒有 willreceiveprops ?
作者: chatnoir (對不起)   2017-08-02 23:39:00
state是元件自己的狀態,元件與元件溝通要用props

Links booklink

Contact Us: admin [ a t ] ucptt.com