簡體   English   中英

如何使用來自reducer的一些數據更新組件的狀態

[英]How to update state of component with some data which is coming from reducer

可以說我有一個subcategoryContainer.js,它將道具傳遞給subcategory.js,現在在subcategory.js中導入了另一個組件filter.js。 此filter.js從subcategory.js接收道具。 數據來自存儲。 在這里,redux thunk正在異步更新存儲。

現在的問題是將道具置於filter.js的狀態。 初始狀態設置為空對象。 即使我在componentWillReceiveProps方法中更新狀態,我也只能在render()中獲得空對象,而不是在應具有道具數據的更新狀態中。

這是觸發方法的順序。

1.render() // initial data from redux reducer//
2.componentWillReceiveProps()
3.render() // new data by redux thunk

在第3點,似乎componentWillReceiveProps()也應該觸發,但不是。

我該如何使用新接收到的道具來更新filter.js狀態(道具->由thunk更新)

我應該怎么做。 請協助。

您確定componentWillReceiveProps實際上已觸發嗎? 它僅在道具實際更改時觸發,而不在組件最初安裝且道具不更改時觸發。 您可能不想將狀態初始化為空狀態,而已將狀態初始化為基於props的東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM