简体   繁体   English

如何让复选框与 React 一起使用?

[英]How do I get checkboxes to work with React?

I'm having trouble getting my checkbox to work for my homework assignment.我无法让我的复选框用于我的家庭作业。 I know it has something to do with a unique key after I'm iterating through my object via jsx, but no matter what combinations I try, the checkbox doesn't work.在我通过 jsx 遍历我的 object 之后,我知道它与唯一键有关,但无论我尝试什么组合,复选框都不起作用。

Object Code Object 代码


export const initialTodoState = [{

    item:"Learn about reducers",
    complete: false,
    id: Date.now()

},

{

    item: "Learn Redux",
    complete: false,
    id: Date.now()
},

{

    item: "Learn UX design",
    complete: false,
    id: Date.now()
}
];

Ul code UL代码

         <ul>
             {filteredTodos.map((todo) => (
          <li key={cuid()}>
            <label>
              <input
              type="checkbox"
                checked={todo.complete}
                onChange={() => handleChanges(todo)}
              />
              {todo.item}
            </label>
          </li>
        ))}
      </ul>

handleChanges code处理更改代码


const handleChanges = todo => {
        dispatch({
          type: todo.complete ? 'UNDO_TODO' :'DO_TODO'  ,
          id: todo.id
        });
      };

Looks like you need to pass todo.id as key.看起来您需要将todo.id作为键传递。 Your element key should not change over render.你的元素键不应该改变渲染。 I recommend you to read the docs more carefully.我建议您更仔细地阅读文档

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

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