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

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

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

问题

希望你可以帮助我。

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

  1. import React, { useState } from "react";
  2. import "./styles.css";
  3. const App = () => {
  4. const [active, setActive] = useState("false");
  5. const creditValues = [
  6. {
  7. month: 6,
  8. monthly: 102.72,
  9. interest: 24.36,
  10. cost: 616.32,
  11. total: 591.96
  12. },
  13. {
  14. month: 12,
  15. monthly: 53.15,
  16. interest: 24.36,
  17. cost: 616.32,
  18. total: 591.96
  19. }
  20. ];
  21. return (
  22. <ul className="monthly-instalments">
  23. {creditValues.map(creditValue => (
  24. <li
  25. className={setActive ? "active" : "notactive"}
  26. data-month={creditValue.month}
  27. data-interval={creditValue.interval}
  28. data-apr={creditValue.apr}
  29. data-monthly={creditValue.monthly}
  30. data-interest={creditValue.interest}
  31. data-cost={creditValue.cost}
  32. data-total={creditValue.total}
  33. onClick={() => setActive(!active)}
  34. >
  35. {creditValue.month} months
  36. </li>
  37. ))}
  38. </ul>
  39. );
  40. };
  41. export default App;

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

英文:

Hope you can help me.

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

  1. import React, { useState } from &quot;react&quot;;
  2. import &quot;./styles.css&quot;;
  3. const App = () =&gt; {
  4. const [active, setActive] = useState(&quot;false&quot;);
  5. const creditValues = [
  6. {
  7. month: 6,
  8. monthly: 102.72,
  9. interest: 24.36,
  10. cost: 616.32,
  11. total: 591.96
  12. },
  13. {
  14. month: 12,
  15. monthly: 53.15,
  16. interest: 24.36,
  17. cost: 616.32,
  18. total: 591.96
  19. }
  20. ];
  21. return (
  22. &lt;ul className=&quot;monthly-instalments&quot;&gt;
  23. {creditValues.map(creditValue =&gt; (
  24. &lt;li
  25. className={setActive ? &quot;active&quot; : &quot;notactive&quot;}
  26. data-month={creditValue.month}
  27. data-interval={creditValue.interval}
  28. data-apr={creditValue.apr}
  29. data-monthly={creditValue.monthly}
  30. data-interest={creditValue.interest}
  31. data-cost={creditValue.cost}
  32. data-total={creditValue.total}
  33. onClick={() =&gt; setActive(!active)}
  34. &gt;
  35. {creditValue.month} months
  36. &lt;/li&gt;
  37. ))}
  38. &lt;/ul&gt;
  39. );
  40. };
  41. export default App;

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

答案1

得分: 3

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

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

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

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

  1. import React, { useState } from 'react';
  2. import './styles.css';
  3. const creditValues = [
  4. {
  5. month: 6,
  6. monthly: 102.72,
  7. interest: 24.36,
  8. cost: 616.32,
  9. total: 591.96,
  10. },
  11. {
  12. month: 12,
  13. monthly: 53.15,
  14. interest: 24.36,
  15. cost: 616.32,
  16. total: 591.96,
  17. },
  18. ];
  19. const Item = ({ children }) => {
  20. const [active, setActive] = useState(false);
  21. return (
  22. <li
  23. className={active ? 'active' : 'noactive'}
  24. onClick={() => setActive(prevActive => !prevActive)}
  25. >
  26. {children}
  27. </li>
  28. );
  29. };
  30. const App = () => {
  31. return (
  32. <ul className="monthly-instalments">
  33. {creditValues.map((creditValue, index) => (
  34. <Item key={index}>{creditValue.month} months</Item>
  35. ))}
  36. </ul>
  37. );
  38. };
  39. 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.

  1. import React, { useState } from &#39;react&#39;;
  2. import &#39;./styles.css&#39;;
  3. const creditValues = [
  4. {
  5. month: 6,
  6. monthly: 102.72,
  7. interest: 24.36,
  8. cost: 616.32,
  9. total: 591.96,
  10. },
  11. {
  12. month: 12,
  13. monthly: 53.15,
  14. interest: 24.36,
  15. cost: 616.32,
  16. total: 591.96,
  17. },
  18. ];
  19. const Item = ({ children }) =&gt; {
  20. const [active, setActive] = useState(false);
  21. return (
  22. &lt;li
  23. className={active ? &#39;active&#39; : &#39;noactive&#39;}
  24. onClick={() =&gt; setActive(p =&gt; !p)}
  25. &gt;
  26. {children}
  27. &lt;/li&gt;
  28. );
  29. };
  30. const App = () =&gt; {
  31. return (
  32. &lt;ul className=&quot;monthly-instalments&quot;&gt;
  33. {creditValues.map((creditValue, index) =&gt; (
  34. &lt;Item key={index}&gt;{creditValue.month} months&lt;/Item&gt;
  35. ))}
  36. &lt;/ul&gt;
  37. );
  38. };
  39. 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:

确定