I have an event handler and every time I click it, my app crashes. The issue is relative to the part after the Else statement. What I want to achieve is : I have a page with 3 buttons once clicked 1 button it adds an object inside the state array and sends the user to the next page where he has other buttons with the same functionality. I want to, if the user clicks “Back” in the browser and clicks again on the button, to overwrite the price of the object relative to that page. I don’t know how to express myself better, sorry.
`import React, { useContext } from "react";
import { PagesContext } from "../model/PagesContext";
import {
MainWrapper,
MainText,
ButtonsWrapper,
Icon,
SelectionsContainer,
ButtonLabel
} from "../StyledComponents";
const Choices = ({ pagename, values }) => {
const [price, setPrice, history] = useContext(PagesContext);
const checker = price.some(item => item.url === history.location.pathname);
const AddPrice = e => {
values.forEach(element => {
if (element.id === e.target.id && !checker) {
setPrice([
...price,
{ price: element.price, url: history.location.pathname }
]);
history.push(element.next);
} else {
setPrice(
price.forEach(obj => {
if (checker && element.id === e.target.id) {
obj.price = element.price;
}
})
);
history.push(element.next);
}
});
};
return (
<MainWrapper>
<MainText>{pagename}</MainText>
<ButtonsWrapper>
{values.map(button => (
<SelectionsContainer key={button.id}>
<Icon
onClick={AddPrice}
src={"/svg-icons/" + button.icon}
id={button.id}
style={{ width: "100px", height: "100px" }}
/>
<ButtonLabel>{button.name}</ButtonLabel>
</SelectionsContainer>
))}
</ButtonsWrapper>
</MainWrapper>
);
};
export default Choices;
`