React状态钩子,切换类,为什么两个元素都获得了”active”类?

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

React state hook, toogle class, why are both elements getting the "active" class?

问题

希望你可以帮助我。

为什么我的两个li元素都被应用了active类?

import React, { useState } from "react";
import "./styles.css";

const App = () => {
  const [active, setActive] = useState("false");

  const creditValues = [
    {
      month: 6,
      monthly: 102.72,
      interest: 24.36,
      cost: 616.32,
      total: 591.96
    },
    {
      month: 12,
      monthly: 53.15,
      interest: 24.36,
      cost: 616.32,
      total: 591.96
    }
  ];

  return (
    <ul className="monthly-instalments">
      {creditValues.map(creditValue => (
        <li
          className={setActive ? "active" : "notactive"}
          data-month={creditValue.month}
          data-interval={creditValue.interval}
          data-apr={creditValue.apr}
          data-monthly={creditValue.monthly}
          data-interest={creditValue.interest}
          data-cost={creditValue.cost}
          data-total={creditValue.total}
          onClick={() => setActive(!active)}
        >
          {creditValue.month} months
        </li>
      ))}
    </ul>
  );
};

export default App;

React状态钩子,切换类,为什么两个元素都获得了”active”类?

英文:

Hope you can help me.

Why are both my li elements getting the active class applied to it?

import React, { useState } from &quot;react&quot;;
import &quot;./styles.css&quot;;

const App = () =&gt; {
  const [active, setActive] = useState(&quot;false&quot;);

  const creditValues = [
    {
      month: 6,
      monthly: 102.72,
      interest: 24.36,
      cost: 616.32,
      total: 591.96
    },
    {
      month: 12,
      monthly: 53.15,
      interest: 24.36,
      cost: 616.32,
      total: 591.96
    }
  ];

  return (
    &lt;ul className=&quot;monthly-instalments&quot;&gt;
      {creditValues.map(creditValue =&gt; (
        &lt;li
          className={setActive ? &quot;active&quot; : &quot;notactive&quot;}
          data-month={creditValue.month}
          data-interval={creditValue.interval}
          data-apr={creditValue.apr}
          data-monthly={creditValue.monthly}
          data-interest={creditValue.interest}
          data-cost={creditValue.cost}
          data-total={creditValue.total}
          onClick={() =&gt; setActive(!active)}
        &gt;
          {creditValue.month} months
        &lt;/li&gt;
      ))}
    &lt;/ul&gt;
  );
};

export default App;

React状态钩子,切换类,为什么两个元素都获得了”active”类?

答案1

得分: 3

在你目前的示例中,两个列表项共享相同的状态。

因此,在每次渲染时,切换状态将反映在两个<li/>项上。

你希望每个列表项都有自己的active状态。

请注意,你有一些错误,如字符串'false',将classNamesetActive函数进行比较,以及没有noactive CSS类。

import React, { useState } from 'react';
import './styles.css';

const creditValues = [
  {
    month: 6,
    monthly: 102.72,
    interest: 24.36,
    cost: 616.32,
    total: 591.96,
  },
  {
    month: 12,
    monthly: 53.15,
    interest: 24.36,
    cost: 616.32,
    total: 591.96,
  },
];

const Item = ({ children }) => {
  const [active, setActive] = useState(false);

  return (
    <li
      className={active ? 'active' : 'noactive'}
      onClick={() => setActive(prevActive => !prevActive)}
    >
      {children}
    </li>
  );
};

const App = () => {
  return (
    <ul className="monthly-instalments">
      {creditValues.map((creditValue, index) => (
        <Item key={index}>{creditValue.month} months</Item>
      ))}
    </ul>
  );
};

export default App;

React状态钩子,切换类,为什么两个元素都获得了”active”类?

英文:

In your current example, both list items share the same state.

Therefore, on each render, toggling the state will reflect both &lt;li/&gt; items.

You want each list item to have its own active state.

Note that you have some bugs like string &#39;false&#39;, comparing the className with setActive function and not having the noactive CSS class.

import React, { useState } from &#39;react&#39;;
import &#39;./styles.css&#39;;

const creditValues = [
  {
    month: 6,
    monthly: 102.72,
    interest: 24.36,
    cost: 616.32,
    total: 591.96,
  },
  {
    month: 12,
    monthly: 53.15,
    interest: 24.36,
    cost: 616.32,
    total: 591.96,
  },
];

const Item = ({ children }) =&gt; {
  const [active, setActive] = useState(false);

  return (
    &lt;li
      className={active ? &#39;active&#39; : &#39;noactive&#39;}
      onClick={() =&gt; setActive(p =&gt; !p)}
    &gt;
      {children}
    &lt;/li&gt;
  );
};

const App = () =&gt; {
  return (
    &lt;ul className=&quot;monthly-instalments&quot;&gt;
      {creditValues.map((creditValue, index) =&gt; (
        &lt;Item key={index}&gt;{creditValue.month} months&lt;/Item&gt;
      ))}
    &lt;/ul&gt;
  );
};

export default App;

React状态钩子,切换类,为什么两个元素都获得了”active”类?

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

发表评论

匿名网友

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

确定