I have two buttons, one increases the counter and one decreases. Each time the counter changes, the component is to change. I have tried to create both functions so I can click either button and loop through each available component (3 components).
The expected function is to display one component at a time and to cycle through them using the buttons.
The increase function almost works, but I don't know why. The decrease function does not work. Code Sandbox.
const increase = () => {
if (counter < 3) {
setCounter(++counter);
console.log(counter);
} else if (counter === 3) {
setCounter(1);
setShow1(!show1);
console.log(counter);
}
if (counter === 2) {
setShow2(!show2);
}
if (counter === 3) {
setShow3(!show3);
}
};
const decrease = () => {
if (counter === 0) {
setCounter(3);
setShow3(!show3);
console.log(counter);
} else if (counter < 3) {
setCounter(--counter);
console.log(counter);
}
if (counter === 2) {
setShow2(!show2);
}
if (counter === 1) {
setShow1(!show1);
}
};