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
Just pass a variable, that you unfold
setLoading: state => (newLoadingState) => {
return {
...state,
loading: {
...state.loading,
...newLoadingState
}
};
}
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 ...
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) ? [...