[英]immutablejs (react/redux) , “push” into state with immutable
在我的react / redux應用程序中,我使用以下內容在reducers中設置狀態:
state.set('search', myResults);
state是一個immutablejs Map()
。 在運行toJS()時對象的格式(不再是不可變對象)是這樣的:
{
results: { ..all results in here }
}
所以你有state.search.results
。
這很好用,但我有一個場景,我需要“推”到這張地圖的搜索部分。 我認為合並將照顧這一點,但它似乎沒有像我想象的那樣工作。 我努力了 :
state.mergeIn('search', singleResult);
以及
state.merge({ 'search': singleResult });
似乎都沒有工作。 預期的結果是將單個結果推入狀態Map()
(而不是像.set
一樣覆蓋它)。 所以期望的結果最終看起來像這樣:
{
results: { singleResult pushed or merged into here with other results }
}
我不確定如何使用immutablejs,任何建議將不勝感激。 謝謝!
您需要使用更新。 合並不會在密鑰中將數組連接在一起。
state.update('search', search => search.concat(singleResult));
還要注意,如果搜索包含一個普通的JS數組 - 那么數組本身仍然是一個可變數據結構。 除非使用fromJS(),否則Immutable.JS不會對數據類型進行深度轉換。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.