简体   繁体   English

单击按钮时我无法指定列表项-React

[英]I cannot specify the list item when I click the button - React

I am currently working on a to-do list.我目前正在制定待办事项清单。 What I am trying to do is that when I click the "completed" button, I want to use the CSS "line-through" property on a list item whichever I want.我想要做的是,当我单击“完成”按钮时,我想在列表项上使用 CSS “line-through”属性,无论我想要什么。 However, when I press the button, all list items are selected.但是,当我按下按钮时,所有列表项都被选中。 How can I get rid of this problem?我怎样才能摆脱这个问题? If the answers will be as code, I would be grateful.如果答案是代码,我将不胜感激。

This is form component:这是表单组件:

const [draw, setDraw] = useState("notDrawed")
const whenClicked = () => {
    setDraw("drawed")
  }

  return (
    <div id="container">
      <h1>todos</h1>
      <form onSubmit={onSubmit} >
        <input placeholder="What needs to be done?" name="todo" value={form.todo} onChange={handleChange} />
        <List forCurrentList={currentList} moveDraw={draw} />
        
        <div> 
          
          <button>Add</button>
          <button type="button">All</button>
          <button type="button">Active</button>
          <button type="button" id="completed" onClick={whenClicked} >
          Completed</button>
          
          </div>
        
        </form>

This is list component which items are listed:这是列出哪些项目的列表组件:

import React, {useState} from "react"

function List({ forCurrentList, moveDraw}) {
  const [active, setActive] = useState(false)

  const handleList = () => {
    setActive(true)
    if (active === true) {
      setActive(false)
    }
  }

  return (
    <div>
      <ul>
        {forCurrentList.map((item, index) => {
          return (
            <React.Fragment key={index}>
              <li className={moveDraw} onClick={handleList} > {item.todo} </li>
            </React.Fragment>
            )
          
          })}
      </ul>
    </div>
  )
}

export default List

Finally, this is the CSS file:最后,这是 CSS 文件:

.notDrawed {
  list-style: none;
  border-bottom: solid 2px rgba(128, 128, 128, 0.672);
  padding-bottom: 2px;
  padding-top: 7px;
  margin-right: 25px;
.drawed {
  list-style: none;
  border-bottom: solid 2px rgba(128, 128, 128, 0.672);
  padding-bottom: 2px;
  padding-top: 7px;
  margin-right: 25px;
  color: grey;
  text-decoration: line-through;
}
}

The completed button and the drawed state are common for all your list items.完成的按钮和绘制的 state 对于您的所有列表项都是通用的。 Try shifting this state toggle to the child to have individual drawed-notDrawed state on each of your list items.尝试将此 state 切换到子项,以便在每个列表项上都有单独的 drawed-notDrawed state。

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

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