JavaScript Updating a nested object with boolean values updating nested object redux,updating a nested object,update nested

I have a loading object, with boolean values for each loading. My state looks like this:

state: {
  loading: {
    itemA: false,
    itemB: false,
    itemC: false
  }
}

I want to update my state using setLoading({ itemA: true }) but having this only update itemA while keeping itemB and itemC the same as whatever the current state is.

return {
  ...state,
  loading: {
    ...state.loading,
    itemA,
    itemB,
    itemC
  }
};

Here is the full reducer (condensed):

setLoading: state => ({ itemA, itemB, itemC }) => {
    return {
      ...state,
      loading: {
        ...state.loading,
        itemA,
        itemB,
        itemC
      }
    };
  }

Unfortunately, if I setLoading({ itemC: true }), A and B are now undefined.

How do I ensure they are not undefined, but rather whatever is in the state?

Please note - I have tried passing just anything and doing object assign or spreading loading.

However, to increase readability, I am wondering if I can destructure the props and not have to define each loading (I have like 12 things on this page that load separately - a dashboard).

Thanks

Answer:1

Just pass a variable, that you unfold

  setLoading: state => (newLoadingState) => {
    return {
      ...state,
      loading: {
        ...state.loading,
        ...newLoadingState
      }
    };
  }
Answer:2

I create a new record, this record is successfully added to the database. That's good. But the problem is that it doesn't update the list of existing records - means the push function doesn't seems to ...

I create a new record, this record is successfully added to the database. That's good. But the problem is that it doesn't update the list of existing records - means the push function doesn't seems to ...

  1. angularjs does not update view
  2. angularjs does not work in ie
  3. angularjs does not show data
  4. angularjs does not contain
  5. angularjs doesn't work
  6. angularjs doesn't work in ie
  7. angularjs doesn't update view
  8. angularjs doesn't work in ie11
  9. angularjs doesnt load
  10. angularjs doesnt render
  11. angularjs doesnt update
  12. angularjs does not bind
  13. angularjs doesnt show data
  14. angularjs does not refresh
  15. angularjs doesnt start
  16. angularjs does not display
  17. angularjs does not recognize
  18. angularjs does not show
  19. angularjs does nothing
  20. angularjs watch doesn't fire

I am trying to use the map function to create an array of the items returned from a collection. My implementation is using the forEach to iterate which works fine. However, I can't get it to work ...

I am trying to use the map function to create an array of the items returned from a collection. My implementation is using the forEach to iterate which works fine. However, I can't get it to work ...

I have a jQuery plugin that does template transformation, and an Angular directive that integrates with the plugin by calling $compile after the plugin runs on the produced DOM node: //in the link ...

I have a jQuery plugin that does template transformation, and an Angular directive that integrates with the plugin by calling $compile after the plugin runs on the produced DOM node: //in the link ...

What is the difference if I use: var numbers = [1, 2, 3] var mainArray = (numbers.length > 1) ? numbers : ''; instead of this: var numbers = [1, 2, 3] var mainArray = (numbers.length > 1) ? [...

What is the difference if I use: var numbers = [1, 2, 3] var mainArray = (numbers.length > 1) ? numbers : ''; instead of this: var numbers = [1, 2, 3] var mainArray = (numbers.length > 1) ? [...

  1. what difference between
  2. what difference between can and could
  3. what difference between will and would
  4. what difference between since and for
  5. what difference between osmosis and diffusion
  6. what difference between aerobic and anaerobic respiration
  7. what difference between while and during
  8. what difference between until and by
  9. what difference between for and during
  10. what's difference between when and while
  11. what the difference when you cc an email
  12. what's the difference when cooking
  13. what time when difference
  14. what the age difference when dating
  15. what is the difference when 93 is subtracted from 387
  16. what interest rate difference when to refinance
  17. what is the difference when 6 is subtracted from 15