简体   繁体   English

如何使用钩子在 React 中编辑我的待办事项?

[英]How can I edit my todo items in React using hooks?

Here is my codesandbox showing the app https://codesandbox.io/s/vibrant-ptolemy-i8f74?file=/src/TodoList.js这是我的代码和框显示应用程序https://codesandbox.io/s/vibrant-ptolemy-i8f74?file=/src/TodoList.js

Right now the code allows me to add, cross out, and delete a todo item, but I want to be able to edit the todos as well.现在,代码允许我添加、划掉和删除待办事项,但我也希望能够编辑待办事项。

I'm not too sure how I would go about it?我不太确定我会怎么做?

This is the current todo item code这是当前的待办事项代码

      const Todo = ({ todos, completeTodo, removeTodo }) => {
        return todos.map(todo => (
          <div className='todo-row'>
            <div
              key={todo.id}
              className={todo.isComplete ? 'complete' : ''}
              onClick={() => completeTodo(todo.id)}
            >
              {todo.text}
            </div>
            <FaWindowClose onClick={() => removeTodo(todo.id)} />
          </div>
        ));
      };

将待办事项文本放入输入中,并使用 todo.id 将其绑定到具有 onChange 的数据模型

https://codesandbox.io/s/interesting-browser-25ciw?file=/src/Todo.js https://codesandbox.io/s/interesting-browser-25ciw?file=/src/Todo.js

  1. add updateTodo function in TodoList.jsTodoList.js添加updateTodo函数
  2. add props.edit in TodoForm.jsprops.edit中添加TodoForm.js
  3. create edit state in Todo.js and change to TodoForm component if edit.id has valueTodo.js创建edit状态并在edit.id有值时更改为TodoForm组件

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

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