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

看板Ajax作者 (重出江湖)時間6年前 (2017/08/01 22:44), 編輯推噓3(302)
留言5則, 4人參與, 最新討論串1/1
請問各位 我用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? 請問這個現象有什麼文章可以學習呢? 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.233.159.254 ※ 文章網址: https://www.ptt.cc/bbs/Ajax/M.1501598669.A.5C3.html

08/01 23:07, , 1F
你是用props傳到child嗎?
08/01 23:07, 1F

08/02 08:41, , 2F
我是用state傳值
08/02 08:41, 2F

08/02 09:51, , 3F
code貼上來阿
08/02 09:51, 3F

08/02 22:54, , 4F
child 有沒有 willreceiveprops ?
08/02 22:54, 4F

08/02 23:39, , 5F
state是元件自己的狀態,元件與元件溝通要用props
08/02 23:39, 5F
文章代碼(AID): #1PW9FDN3 (Ajax)