如何在React组件中单击按钮时恢复按钮的状态

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

How to revert the state of a button when clicked in a React component

问题

尝试在按钮被点击时恢复颜色和图标,但似乎无法找到处理状态的良好方法。
颜色和图标只改变一次,但再次点击后似乎无法恢复到原始状态。
我只想在每次点击时能够来回切换颜色和图标。

到目前为止,我尝试了以下方法...

英文:

Am trying to revert the color and icon of a button when clicked, but I can't seem to come up with a good approach to deal with the state
The the color and icon only change one time but can't seem to revert back to the original once clicked again
I just want to be able to switch the color and icon back and forth on every click

Here's what I've tried so far...

code in vs

答案1

得分: 1

The issue is that your function is setting the icon to a tick and the color to green. Every time you click it, it will set it to the same value. I would remove both of these states and put only one - a boolean value that checks the state of the button. If it is true, the icon should be a tick, and the color should be green. If it is false - a cross and red.

  1. const [isClicked, setIsClicked] = useState(false)
  2. return (
  3. <main>
  4. <section
  5. className={isClicked ? 'green' : 'red'}
  6. onClick={() => setIsClicked(!isClicked)}
  7. >
  8. {isClicked ? 'tick' : 'cross'}
  9. </section>
  10. </main>
  11. )
英文:

The issue is that your function is setting the icon to a tick and the color to green. Every time you click it it will set it to the same value. I would remove both of this states and put only one - boolean value that checks the state of the button. If it is true, the icon should be a tick and color should be green. If it is false - cross and red.

  1. const [isClicked, setIsClicked] = useState(false)
  2. return (
  3. &lt;main&gt;
  4. &lt;section
  5. className={isClicked ? &#39;green&#39; : &#39;red&#39;}
  6. onClick={() =&gt; setIsClicked(!isClicked)}
  7. &gt;
  8. {isClicked ? *tick* : *cross*}
  9. &lt;/section&gt;
  10. &lt;/main&gt;
  11. )

答案2

得分: 0

这是您提供的JavaScript代码的中文翻译:

  1. // 这是我刚想出的类似解决方案
  2. function App() {
  3. const [isOpen, setIsOpen] = useState(true);
  4. const 切换符号 = () => {
  5. setIsOpen(s => !s);
  6. }
  7. return (
  8. <>
  9. {
  10. isOpen ?
  11. <main>
  12. <section className="red" onClick={切换符号}></section>
  13. </main>
  14. : <main>
  15. <section className="green" onClick={切换符号}></section>
  16. </main>
  17. }
  18. </>
  19. )
  20. }

如果您需要更多帮助或有其他问题,请随时告诉我。

英文:
  1. // Here&#39;s a similar solution I just came up with
  2. function App() {
  3. const [isOpen, setIsOpen] = useState(true);
  4. const symbol = () =&gt; {
  5. setIsOpen(s =&gt; !s);
  6. }
  7. return (
  8. &lt;&gt;
  9. {
  10. isOpen ?
  11. &lt;main&gt;
  12. &lt;section className=&quot;red&quot; onClick={symbol}&gt;&lt;/section&gt;
  13. &lt;/main&gt;
  14. : &lt;main&gt;
  15. &lt;section className=&quot;green&quot; onClick={symbol}&gt;&lt;/section&gt;
  16. &lt;/main&gt;
  17. }
  18. &lt;/&gt;
  19. )
  20. }

huangapple
  • 本文由 发表于 2023年6月15日 16:37:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76480634.html
匿名

发表评论

匿名网友

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

确定