英文:
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 "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>
)
}
If anyone could tell me how to correctly update the buttons I would really apreciate it.
Thanks!
答案1
得分: 1
你正在将你的 button
用作 input type="radio"
一样。按钮的 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="radio"
. 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="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 "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>
)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论