How to Activate Toggle Functionality
Currently, I am working on a project using React where I developed a list with active states. I have implemented the onClick
functions, but unfortunately, they are not working as intended. When I click on the list items, all I see is a quick flash of the background color that should indicate the active state of the div.
import React, { useState } from 'react';
import './Home.css';
function Home() {
const [isActive, setIsActive] = useState({
oneStar: true,
twoStars: false,
threeStars: false,
fourStars: false,
fiveStars: false,
});
return (
<div className='app_card_container'>
<div className='app_card_body'>
<div className='app_card_list'>
<div
className={isActive.oneStar
? 'app_card_lists active'
: 'app_card_lists'
}
onClick={() =>
setIsActive({
oneStar: true,
twoStars: false,
threeStars: false,
fourStars: false,
fiveStars: false,
})
}
>
1
</div>
<div
className={isActive.twoStars
? 'app_card_lists active'
: 'app_card_lists'
}
onClick={() =>
setIsActive({
oneStar: false,
twoStars: true,
threeStars: false,
fourStars: false,
fiveStars: false,
})
}
>
2
</div>
<div
className={isActive.threeStars
? 'app_card_lists active'
: 'app_card_lists'
}
onClick={() =>
setIsActive({
oneStar: false,
twoStars: false,
threeStars: true,
fourStars: false,
fiveStars: false,
})
}
>
3
</div>
<div
className={isActive.fourStars
? 'app_card_lists active'
: 'app_card_lists'
}
onClick={() =>
setIsActive({
oneStar: false,
twoStars: false,
threeStars: false,
fourStars: true,
fiveStars: false,
})
}
>
4
</div>
<div
className={isActive.fiveStars
? 'app_card_lists active'
: 'app_card_lists'
}
onClick={() =>
setIsActive({
oneStar: false,
twoStars: false,
threeStars: false,
fourStars: false,
fiveStars: true,
})
}
>
5
</div>
</div>
</div>
</div>
);
}
export default Home;
For the CSS regarding the involved component, here is what I have implemented:
.app_card_list {
display: flex;
flex-direction: row;
}
.app_card_lists {
margin-left: 1.5rem;
display: flex;
flex-direction: row;
justify-content: center;
width: 35px;
height: 35px;
background-color: hsl(216, 12%, 8%);
text-align: center;
align-items: center;
border-radius: 50%;
cursor: pointer;
color: hsl(0, 0%, 100%);
}
.app_card_lists:hover {
background-color: hsl(217, 12%, 63%);
}
.app_card_lists:active {
background-color: red;
}