Reputation: 1596
I have an app-state object, which sort of looks like this:
const initialState = Immutable.fromJS({
$isLoading: true,
currentPage: 0,
totalPages: 1,
results: []
});
When I fetch from server (using fetch
API), I want to set $isLoading
to false, and merge currentPage
and totalPages
in from the response. That's easy enough so far using merge
. I'm not quite sure how to elegantly merge in the results
node. They're Immutable.Map
s, and should merge based on the _id
property. Is there an elegant way to work it into this bit of code?
return state.merge({
$isLoading: false,
currentPage: res.page,
totalPages: res.pages,
results: 'something' // Take old results, merge with res.results
});
Upvotes: 4
Views: 9527
Reputation:
You should just be able to change your merge function to mergeDeep:
https://facebook.github.io/immutable-js/docs/#/Map/mergeDeep
Upvotes: 4