简体   繁体   English

在 preact 中重用“add todo”组件作为“edit todo”

[英]reusing the “add todo” component as “edit todo” in preact

I just copied the todo from w3 schools and added some functionality with preact.我刚刚从 w3 学校复制了 todo,并使用 preact 添加了一些功能。 So far all working fine with functional components.到目前为止,所有功能组件都可以正常工作。 Don't mind the string literals and the inline styles.不要介意字符串文字和内联 styles。

What I am not getting is:我没有得到的是:

I have the empty "form" for adding todos:我有用于添加待办事项的空“表单”:

    const EditTodo = (props) => {
    return html`
      <h2 style="margin:5px;">My To Do List</h2>
      <input ref=${todoTitleRef} type="text" placeholder="Title..." value=${props.todo.title}/>
      <textarea value=${props.todo.content} ref=${todoContentRef} style="margin-top: 20px;" type="text" id="myText" placeholder="Text..."></textarea>
    `;
  }

I wanted to reuse it for editing a todo.我想重用它来编辑待办事项。 So my question is: How can I add a hook in the root component to re-render the EditTodo but passing the todo that was clicked to edit?所以我的问题是:如何在根组件中添加一个钩子来重新渲染 EditTodo 但传递被点击编辑的 todo?

this is the main component:这是主要组成部分:

return html`
  <div class="app">
    <div id="myDIV" class="header">
      ${html`<${EditTodo} todo=${{}}/>`}
      <div style="margin-top: 40px;"><span class="addBtn" onClick=${addTodo}>Add</span></div>
    </div>
    <ul id="myUL">
      ${todos.map((todo) => html`<${TodoItem} todo=${todo}/>`)}
    </ul>
  </div>
  `;

For anyone wondering,对于任何想知道的人,

added the const after the reducer:在 reducer 之后添加了 const:

  const editingTodo = todos.filter(todo=>todo.isEditing);

then changed the prop todo of the main editTodo to:然后将主editTodo的prop todo更改为:

  ${html`<${EditTodo} todo=${editingTodo.length>0?editingTodo[0]:{}}/>`}

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

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