简体   繁体   English

反应复选框值未正确显示

[英]React Checkbox Value Not Showing Correctly

I'm trying to create a simple todo list app.我正在尝试创建一个简单的待办事项列表应用程序。 The problem is that when I render the checkbox this does not check itself depending on the value but set itself to checked always.问题是,当我渲染复选框时,它不会根据值检查自身,而是将自身设置为始终检查。 I have tried to change the value to "defaultChecked" instead of "checked" but this does not seem to work.我试图将值更改为“defaultChecked”而不是“checked”,但这似乎不起作用。 Also I have tried to test the app in Edge,Chrome,Mozila... And all of them show up the same error.我还尝试在 Edge、Chrome、Mozila 中测试该应用程序......并且所有这些都显示相同的错误。 Here's the code of App.jsx :这是App.jsx的代码:

import React from "react"

const wishes = [
{name: "Travel to the moon",
done:"false"},

{name:"Pay the gym",
done:"true"},

{name:"Go to the gym",
done:"false"}
]

const App = () => <div>
<h1>My wishlist</h1>
<fieldset>
    <legend>New Wish</legend>
    <input placeholder="Enter wish here"></input>
</fieldset>
<ul>
    {wishes.map(({name,done})=>(
        <li>
            <label>{name}:{done}</label>
            <input type="checkbox" defaultChecked={done}/>
        </li>
    ))}
</ul>
<button type="button">Archive done</button>
</div>

export default App;

Thank you very much!非常感谢!

const wishes = [
{name: "Travel to the moon",
done:"false"},

{name:"Pay the gym",
done:"true"},

{name:"Go to the gym",
done:"false"}
]

done is a string which would be converted to true when used as the defaultChecked value of the checkbox. done是一个字符串,当用作复选框的defaultChecked值时将转换为true You need to use a boolean done: true or done: false您需要使用 boolean done: truedone: false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM