按钮组从列表中呈现,但在单击后未重新呈现,其值返回未定义。

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

Group of buttons rendered from list are not being re-rendered on click and their value returns undefined

问题

我正在尝试创建一个困难度设置,您可以从4个选项中选择困难度,这些选项以按钮组的形式排列,我还想在选定的按钮上放置一个边框,并且在单击时更改困难度,从按钮的值获取困难度。问题是,单击按钮时按钮不会更改,并且由于某种原因其值变为未定义。

以下是我的代码:

import { useState } from "react";
import { config } from "../config";

const difficulties: Difficulty[] = ["Easy", "Normal", "Hard", "Extreme"];

export default function DifficultySettingsComponent() {
  const [difficulty, setDifficulty] = useState(config.difficulty);

  function changeDificulty(event: any) {
    setDifficulty(event.target.value);
    config.difficulty = difficulty;
  }

  return (
    <div>
      <h2 className="font-bold text-lg">Difficulty:</h2>
      <div className="space-x-2 m-3">
        {difficulties.map((difficulty) => (
          <button className="bg-zinc-400 px-3 py-2 border-black rounded-md"
            key={difficulty}
            onClick={changeDificulty}
            value={difficulty}
            style={{ borderWidth: difficulty == config.difficulty ? 2 : 0 }}
          >
            <p className="font-bold">{difficulty}</p>
          </button>
        ))}
      </div>
    </div>
  );
}

如果有人能告诉我如何正确更新按钮,我会非常感激。谢谢!

英文:

I'm trying to create a difficulties setting where you can choose the difficulty from 4 options laid out in a group of buttons, I also want to put a border on the selected button and have the difficulty change on click, getting the difficulty from the value of the button. The problem is that the button isn't changing on click and the value of it becomes undefined for some reason.

Here's my code:

import { useState } from &quot;react&quot;;
import { config } from &quot;../config&quot;;

const difficulties: Difficulty[] = [&quot;Easy&quot;, &quot;Normal&quot;, &quot;Hard&quot;, &quot;Extreme&quot;]

export default function DifficultySettingsComponent() {
  const [difficulty, setDifficulty] = useState(config.difficulty)

  function changeDificulty(event: any) {
    setDifficulty(event.target.value)
    config.difficulty = difficulty
  }

  return (
    &lt;div&gt;
      &lt;h2 className=&quot;font-bold text-lg&quot;&gt;Difficulty:&lt;/h2&gt;
      &lt;div className=&quot; space-x-2 m-3&quot;&gt;
        {difficulties.map((difficulty) =&gt; (
          &lt;button className=&quot; bg-zinc-400 px-3 py-2 border-black rounded-md&quot;
            key={difficulty}
            onClick={changeDificulty}
            value={difficulty}
            style={{ borderWidth: difficulty == config.difficulty ? 2 : 0 }}
          &gt;
            &lt;p className=&quot;font-bold&quot;&gt;{difficulty}&lt;/p&gt;
          &lt;/button&gt;
        ))}
      &lt;/div&gt;
    &lt;/div&gt;
  )
}

按钮组从列表中呈现,但在单击后未重新呈现,其值返回未定义。

If anyone could tell me how to correctly update the buttons I would really apreciate it.
Thanks!

答案1

得分: 1

你正在将你的 button 用作 input type=&quot;radio&quot; 一样。按钮的 value 不会产生你期望的效果。

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button

在这种情况下,你可能应该考虑使用样式类似按钮的单选按钮。这样更符合常规表单的风格。例如,如果你正在使用 Bootstrap,你可以使用“切换按钮” https://getbootstrap.com/docs/5.0/forms/checks-radios/#toggle-buttons

希望这为你提供了一个开始的地方!

英文:

You're using your button as though it was an input type=&quot;radio&quot;. The value of a button doesn't do what you're expecting.

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button

In this scenario, you should probably aim to use radio buttons that are styled to look like buttons. It will be more in line with a regular form that way. For example, if you're using Bootstrap, you can use "Toggle Buttons" https://getbootstrap.com/docs/5.0/forms/checks-radios/#toggle-buttons

Hopefully this gives you a place to start!

答案2

得分: 0

我根据@Willow的答案将按钮更改为input type="button",现在它正确地接受了value。我还不得不更改边框的条件语句,它也起作用了,下面是我的新工作代码:

import { useState } from "react";
import { config } from "../config";

const difficulties: Difficulty[] = ["Easy", "Normal", "Hard", "Extreme"];

export default function DifficultySettingsComponent() {
  const [currentDifficulty, setCurrentDifficulty] = useState(config.difficulty);

  function changeDificulty(event: any) {
    setCurrentDifficulty(event.target.value);
    config.difficulty = currentDifficulty;
  }

  return (
    <div>
      <h2 className="font-bold text-lg">Difficulty:</h2>
      <div className="space-x-2 m-3">
        {difficulties.map((difficulty) => (
          <input
            className="bg-zinc-400 px-3 py-2 border-black rounded-md"
            type="button"
            key={difficulty}
            onClick={changeDificulty}
            value={difficulty}
            style={{ borderWidth: difficulty == currentDifficulty ? 2 : 0 }}
          />
        ))}
      </div>
    </div>
  );
}
英文:

I changed the button to an input type=&quot;button based on @Willow's answer and it now correctcly takes the value. I also had to change the if statement for the border and it worked, here's my new working code:

import { useState } from &quot;react&quot;;
import { config } from &quot;../config&quot;;

const difficulties: Difficulty[] = [&quot;Easy&quot;, &quot;Normal&quot;, &quot;Hard&quot;, &quot;Extreme&quot;]

export default function DifficultySettingsComponent() {
  const [currentDifficulty, setCurrentDifficulty] = useState(config.difficulty)

  function changeDificulty(event: any) {
    setCurrentDifficulty(event.target.value)
    config.difficulty = currentDifficulty
  }

  return (
    &lt;div&gt;
      &lt;h2 className=&quot;font-bold text-lg&quot;&gt;Difficulty:&lt;/h2&gt;
      &lt;div className=&quot; space-x-2 m-3&quot;&gt;
        {difficulties.map((difficulty) =&gt; (
          &lt;input className=&quot; bg-zinc-400 px-3 py-2 border-black rounded-md&quot;
            type=&quot;button&quot;
            key={difficulty}
            onClick={changeDificulty}
            value={difficulty}
            style={{ borderWidth: difficulty == currentDifficulty ? 2 : 0 }}
          /&gt;
        ))}
      &lt;/div&gt;
    &lt;/div&gt;
  )
}

huangapple
  • 本文由 发表于 2023年6月13日 01:32:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459020.html
匿名

发表评论

匿名网友

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

确定