2

I have a immutable state in reducer as:

const initialState = fromJS({
  rates: [], 
  fromDialogOpen: false, 
  mealPlans: [], 
  roomTypes: [], 
  editRateFormOpen: false, 
  rateToEdit: "", 
  selectedDate: moment().toDate(), 
  selectedRoomType: null, 
  selectedMealPlan: null 
});

I do an api call that results me either an empty array [] or array of objects

[
    0: Object { doubleRackRate: 200, singleRackRate: 120, mealPlan: "AI", … }  
    1: Object { doubleRackRate: 12, singleRackRate: 1200, mealPlan: "AI", … }
    2: Object { doubleRackRate: 12, singleRackRate: 12, mealPlan: "AI", … }
    3: Object { doubleRackRate: 12, singleRackRate: 12, mealPlan: "AI", … }             
    4: Object { doubleRackRate: 12, singleRackRate: 12, mealPlan: "AI", … }   
]

sent as action.payload for state update!

As I receive the array from API call I need to update my rates- state to the value I receive - i.e. either an empty array or the array of objects !!

How can I do this? I tried:

return Object.assign( ...state, {rates:action.payload})

But this didn't work !!

1
  • Try: return Object.assign({}, state, { rates: action.payload || [] }); . Hope your action.payload is containing the resulted data/array. Commented Jun 17, 2018 at 8:15

1 Answer 1

3

You've got the right idea, but your syntax is wrong.

Option 1

Use Object.assign to change only the rates field.

return Object.assign(state, {rates:action.payload})

Some may consider this unorthodox, since it changes state directly.

Option 2

Return a copy of state with only rates changed using spread syntax.

return { ...state, rates: action.payload }

Further reading: JS spread syntax

For immutable.js

const { set } = require('immutable')
return set(state, 'rates', action.payload)

Immutable.js documentation explains it in more detail.

Sign up to request clarification or add additional context in comments.

5 Comments

This did not work for me !! I could update the array with merge as : return state .update('rates',rates=>rates.merge(action.payload)); but this is not what I desired.
Object.update is not a function. Are you sure state is an object?
I infact have state as : const initialState = fromJS({ rates:[], fromDialogOpen: false, mealPlans: [], roomTypes:[], editRateFormOpen:false, rateToEdit:"", selectedDate:moment().toDate(), selectedRoomType:null, selectedMealPlan:null }); and I need to update the rates:[] only.
As I suspected, state is not a vanilla object, but rather an immutable.js immutable object. You had omitted this tidbit in your question. I added a solution for immutable.js users to my answer.
this worked for me : return state .set('rates', action.payload) though return set(state, 'rates', action.payload) gave me errors !! Thank you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.