简体   繁体   English

发送到另一个容器时删除属性元素

[英]removing an attribute element when sent to another container

I am working on a todo list.我正在处理一个待办事项列表。 When the user clicks the check box it appears in another container but when that happens I want that checkbox to disappear.当用户单击复选框时,它会出现在另一个容器中,但是当发生这种情况时,我希望该复选框消失。

import React from 'react';

class Todo extends React.Component {
  state = {
    checked: false,
  };

  handleCheck = () => {
    this.setState({
      checked: !this.state.checked,
    });
  };

  handleClick = () => {
    this.props.handlecompletedList(this.props.title);
  };

  render() {
    const { title } = this.props;
    return (
      <div className="ui checked checkbox">
        <input type="checkbox" checked={this.state.checked} onChange={this.handleCheck} onClick={this.handleClick} />
        <label>Completed {title}</label>
      </div>
    );
  }
}
export default Todo;
import React from 'react';
import Todo from './Todo';

const Active = props => {
  const { items, handleComplete } = props;

  return (
    <div id="activeList">
      <h2 className="position">Active</h2>
      <ul id="tasks">
        {items.map(item => {
          return <Todo key={item.id} handlecompletedList={handleComplete} title={item.title} />;
        })}
      </ul>
    </div>
  );
};
import React from 'react';
import Todo from './Todo';

const Completed = props => {
  const { completedItems } = props;

  return (
    <div id="completedList">
      <h2 className="position">Completed</h2>
      <ul id="tasks">
        {completedItems.map(item => {
          return <Todo key={item.id} title={item.title} />;
        })}
      </ul>
    </div>
  );
};
export default Completed;

The active is the current todo and when the user clicks the checkbox it appears in the completed container. active 是当前的待办事项,当用户单击复选框时,它会出现在已完成的容器中。

Hannah, I think you should use state in the parent component and provide state and state change function to the children by props. Hannah, I think you should use state in the parent component and provide state and state change function to the children by props.

Example workflow:示例工作流程:

Parent:家长:

...
const [status, setStatus] = useState(0);  // status : 0
...
<child1 status={status} onChange={setStatus} />
<child2 status={status} onChange={setStatus} />
<child3 status={status} onChange={setStatus} />
...

child1: when checking the input call props.onChange(1); child1 检查输入时调用props.onChange(1); // parent status changed and redraw all child: 1 //父状态改变并重绘所有子:1

child2: when checking the input call props.onChange(2); child2 检查输入时调用props.onChange(2); // parent status changed and redraw all child: 2 //父状态改变并重绘所有子:2

child3: when checking the input call props.onChange(3); child3:检查输入时调用props.onChange(3); // parent status changed and redraw all child: 3 //父状态改变并重绘所有子:3

you should make the components like this.你应该像这样制作组件。 then I think it will work.那么我认为它会起作用。

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

相关问题 在容器中可见时从元素中删除css类 - Removing css class from element when visible in container 当另一个span元素包含特定文本时,删除同级div - Removing sibling div when another span element contains specific text 当 class 转到 javascript 中的另一个元素时,从元素中删除 class - removing class from element when the class goes to another one in javascript 将 HTML 元素属性传递给另一个元素属性 - Pass on HTML element attribute to another element attribute 在具有另一个href属性的元素内时,标签无法工作 - a tags dont work when inside element with another href attribute 如何将当前类添加到单击的元素并在单击其他元素时将其删除? - How can I add current class to a clicked element and removing it when clicking on another element? 使用 CDKdraganddrop 将容器的一个元素拖动到另一个元素时,是否可以保留原始容器的样式 - Is it possible to keep the styling of the original container when dragging one element of a container to another using CDKdraganddrop HTML元素没有足迹,但可以是另一个元素的容器 - HTML element with no footprint, but can be a container for another element 根据另一个元素的data属性隐藏元素 - Hide an element based on the data attribute of another element 元素正在从另一个元素中获取属性值 - element is taking attribute values from another element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM