React列表的onClick函数未触发活动切换,如何进行故障排除?

huangapple go评论71阅读模式
英文:

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 &#39;react&#39;;
import &#39;./Home.css&#39;;

function Home() {
  const [isActive, setIsActive] = useState({
    oneStar: true,
    twoStars: false,
    threeStars: false,
    fourStars: false,
    fiveStars: false,
  });

  return (
    &lt;div className=&#39;app_card_container&#39;&gt;
      &lt;div className=&#39;app_card_body&#39;&gt;
        &lt;div className=&#39;app_card_list&#39;&gt;
          &lt;div
            className={isActive.oneStar
              ? &#39;app_card_lists active&#39;
              : &#39;app_card_lists&#39;
            }
            onClick={() =&gt;
              setIsActive({
                oneStar: true,
                twoStars: false,
                threeStars: false,
                fourStars: false,
                fiveStars: false,
              })
            }
          &gt;
            1
          &lt;/div&gt;
          &lt;div
            className={isActive.twoStars
              ? &#39;app_card_lists active&#39;
              : &#39;app_card_lists&#39;
            }
            onClick={() =&gt;
              setIsActive({
                oneStar: false,
                twoStars: true,
                threeStars: false,
                fourStars: false,
                fiveStars: false,
              })
            }
          &gt;
            2
          &lt;/div&gt;
          &lt;div
            className={isActive.threeStars
              ? &#39;app_card_lists active&#39;
              : &#39;app_card_lists&#39;
            }
            onClick={() =&gt;
              setIsActive({
                oneStar: false,
                twoStars: false,
                threeStars: true,
                fourStars: false,
                fiveStars: false,
              })
            }
          &gt;
            3
          &lt;/div&gt;
          &lt;div
            className={isActive.fourStars
              ? &#39;app_card_lists active&#39;
              : &#39;app_card_lists&#39;
            }
            onClick={() =&gt;
              setIsActive({
                oneStar: false,
                twoStars: false,
                threeStars: false,
                fourStars: true,
                fiveStars: false,
              })
            }
          &gt;
            4
          &lt;/div&gt;
          &lt;div
            className={isActive.fiveStars
              ? &#39;app_card_lists active&#39;
              : &#39;app_card_lists&#39;
            }
            onClick={() =&gt;
              setIsActive({
                oneStar: false,
                twoStars: false,
                threeStars: false,
                fourStars: false,
                fiveStars: true,
              })
            }
          &gt;
            5
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  );
}

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 &quot;active&quot; CSS classname.

className={isActive.oneStar ? &#39;app_card_lists active&#39; : &#39;app_card_lists&#39;}

When an item is "active" you are coding it to have both an &quot;app_card_lists&quot; and an &quot;active&quot; 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:

&lt;div 
className={activeStars &lt;= 1 ? &#39;app_card_lists active&#39; : &#39;app_card_lists&#39;} 
onClick={() =&gt; setActiveStars(1)}
&gt;
1
&lt;/div&gt;
&lt;div 
className={activeStars &lt;= 2 ? &#39;app_card_lists active&#39; : &#39;app_card_lists&#39;} 
onClick={() =&gt; setActiveStars(2)}
&gt;
2
&lt;/div&gt;
&lt;div 
className={activeStars &lt;= 3 ? &#39;app_card_lists active&#39; : &#39;app_card_lists&#39;} 
onClick={() =&gt; setActiveStars(3)}
&gt;
3
&lt;/div&gt;
&lt;div 
className={activeStars &lt;= 4 ? &#39;app_card_lists active&#39; : &#39;app_card_lists&#39;} 
onClick={() =&gt; setActiveStars(4)}
&gt;
4
&lt;/div&gt;
&lt;div 
className={activeStars &lt;= 5 ? &#39;app_card_lists active&#39; : &#39;app_card_lists&#39;} 
onClick={() =&gt; setActiveStars(5)}
&gt;
5
&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年6月1日 00:59:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375804.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定