英文:
React list onClick function isn't triggering active toggle, how to troubleshoot?
问题
Making the active toggle on
我正在使用React工作,我创建了一个列表,其中它们将具有活动状态,我已经创建了onClick
函数,但它不起作用。如果我尝试点击并期望切换,我只会得到背景颜色的一闪,我期望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;
这是涉及主题的CSS:
.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;
}
英文:
Making the active toggle on
I am currently working with React and I made a list where they will have active states and I already made the onClick
functions but it won't work. If I try to click and expect the toggle all I get is just flash of the background color I expect the div to get when it's active.
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;
and here is my CSS for the div involved in the subject:
.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;
}
答案1
得分: 1
一个":active"状态/属性与一个"active"
CSS类名不是同一回事。
className={isActive.oneStar ? 'app_card_lists active' : 'app_card_lists'}
当一个项目处于"active"状态时,你将其编码为具有一个"app_card_lists"
和 一个"active"
类名。CSS规则可能更像以下示例:
.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%);
}
.active { /* active CSS类 */
background-color: red;
}
如果要限制active CSS规则的范围,可以提供更具体的选择器。
.app_card_lists.active { /* 同时包含app_card_lists和active CSS类 */
background-color: red;
}
英文:
An ":active" state/attribute is not the same thing as an "active"
CSS classname.
className={isActive.oneStar ? 'app_card_lists active' : 'app_card_lists'}
When an item is "active" you are coding it to have both an "app_card_lists"
and an "active"
classname. The CSS rules should likely be something more like the following:
.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%);
}
.active { /* active CSS class */
background-color: red;
}
If you want to limit the scope of the active CSS rule you can provide a more specific selector.
.app_card_lists.active { /* both app_card_lists and active CSS classes */
background-color: red;
}
答案2
得分: 0
以下是翻译的部分:
实际上,在你的代码中,
app_card_lists active
意味着在你的 CSS 文件中,你应该有一个名为 "active" 的类,如下所示:
.active {
background-color: red;
}
正如你在代码中所写的那样,你正在检查元素 div 是否没有处于 :active 状态,而 div 元素没有这种状态。
资源 mdn 资源
英文:
Actually in your code
app_card_lists active
implies that in your css file you should have a class called active as:
.active {
background-color: red;
}
As you wrote in your code, you are checking if the element div hasn’t a :active state, which div element has’t.
Resources mdn resources
答案3
得分: 0
将您的状态设置如下:
const [activeStars, setActiveStars] = useState(1)
将可点击的星星设置如下:
<div
className={activeStars <= 1 ? 'app_card_lists active' : 'app_card_lists'}
onClick={() => setActiveStars(1)}
>
1
</div>
<div
className={activeStars <= 2 ? 'app_card_lists active' : 'app_card_lists'}
onClick={() => setActiveStars(2)}
>
2
</div>
<div
className={activeStars <= 3 ? 'app_card_lists active' : 'app_card_lists'}
onClick={() => setActiveStars(3)}
>
3
</div>
<div
className={activeStars <= 4 ? 'app_card_lists active' : 'app_card_lists'}
onClick={() => setActiveStars(4)}
>
4
</div>
<div
className={activeStars <= 5 ? 'app_card_lists active' : 'app_card_lists'}
onClick={() => setActiveStars(5)}
>
5
</div>
英文:
set your state like this:
const [activeStars, setActiveStars] = useState(1)
and your clickable Star like this:
<div
className={activeStars <= 1 ? 'app_card_lists active' : 'app_card_lists'}
onClick={() => setActiveStars(1)}
>
1
</div>
<div
className={activeStars <= 2 ? 'app_card_lists active' : 'app_card_lists'}
onClick={() => setActiveStars(2)}
>
2
</div>
<div
className={activeStars <= 3 ? 'app_card_lists active' : 'app_card_lists'}
onClick={() => setActiveStars(3)}
>
3
</div>
<div
className={activeStars <= 4 ? 'app_card_lists active' : 'app_card_lists'}
onClick={() => setActiveStars(4)}
>
4
</div>
<div
className={activeStars <= 5 ? 'app_card_lists active' : 'app_card_lists'}
onClick={() => setActiveStars(5)}
>
5
</div>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论