I believe my problem is originating from the disableButton function, but I can't wrap my head around what is wrong
if the sum of all the values of each content is equal to zero purchasable should be false meaning the button should be disabled but even when I add contents to the cart it is still disabled and sometimes a specific number of values needs to be gotten to make button not-disabled and then it goes back to disabled afterward any help would be appreciated :)
const App = props => {
const [items, setItems] = useState({
contents: {
bacon: 0,
cheese: 0,
},
purchasable: false, /* this is to set the disabled state of the purchase button to true || false */
// ...
}) /* i set my states this way to avoid calling const multiple times is that a bad practice ? */
/* function gets an outdated content so it takes a parameter from the 2 buttons that are going to be updating the state the most adding and removing functions */
const disableButton = (updatedContent) => {
const contentsSum = Object.values(updatedContent).reduce((acc, curr) => (acc + curr), 0)
setItems(items => ({
...items,
purchasable: contentSum > 0 /* idk if something is wrong with this "check" as button gets disabled even when cart isnt empty */
}))
}
/* this is to add contents to a cart */
const addContent(type) => {
updatedContents = {...items.contents}
updatedContents[type] = contentCount + 1
setItems(items => ({
...items,
content: updatedContents
}))
disableButton(updatedContent)
}
/* this removes content from the cart as long as it isnt empty */
const removeContent(type) => {
const contentCount = items.contents[type]
if(contentCount < 0) return
updatedContents = {...items.contents}
updatedContents[type] = contentCount - 1
setItems(items => ({
...items,
content: updatedContents
}))
disableButton(updatedContents)
}
return (
<div>
<ContentControls purchasable={items.purchasable} /> /* the purchasable state is passes to a "place order" that gets disabled based on items.purchasable's state */
</div>
)
};