简体   繁体   English

ReactJS-如何通过使用子按钮删除整个组件

[英]ReactJS - How to remove whole component , by using child button

I have problem , i want to create TODO app in ReactJS and there is functionality like this: 我有问题,我想在ReactJS中创建TODO应用,并且有这样的功能:

  1. onClick is showing/hidding AddTaskBox (form in div, where you have to write title and describtion ) - I done it. onClick正在显示/隐藏AddTaskBox(div中的表单,您必须在其中编写标题和说明)-我做到了。
  2. In visable AddTaskBox is button with "x" and I want to use him to delete whole "AddTaskBox" - Can't figur it out :( 在可见的AddTaskBox中是带有“ x”的按钮,我想用他删除整个“ AddTaskBox”-无法确定:(

I'm greeny in React , my thinking about that library may be wrong. 我对React感到困惑,对那个库的想法可能是错误的。

My code: 我的代码:

import React from 'react';
import ReactDOM from 'react-dom';

class AddButton extends React.Component{

  constructor(){
      super();

      this.state = {
      isHidden: false
    }
  }

  toggleHidden () {
      this.setState({
        isHidden: !this.state.isHidden
      })
  }

  render(){
    return(
      <div>
        <div
          onClick={this.toggleHidden.bind(this)}
          className="addtask__btn">
          +
        </div>
        {this.state.isHidden && <AddTaskBox />}
      </div>
    );
  }
}

class AddTaskBox extends React.Component{
  render(){
    return(
      <div className="addtask__box" >
        <button> x </button> // here is my x to close component
        <form>
          <input type="text" placeholder="Nazwa Czynności"/>
          <textarea>
            Opis czynności do wykonania
          </textarea>
          <input className="btn" type="Submit" value="Submit"/>
        </form>
      </div>
    );
  }
}

export {AddButton, AddTaskBox};

Thanks for help :) 感谢帮助 :)

Assuming by AddTaskComponent you mean AddTaskBox you would pass the toggleHidden function to AddTaskBox 假设AddTaskComponent表示AddTaskBox ,则将toggleHidden函数传递给AddTaskBox

{this.state.isHidden && <AddTaskBox onClose={this.toggleHidden.bind(this)} />}

and then call the function from the child on click 然后在点击时从子级调用该函数

<button onClick={this.props.onClose}> x </button>

You can pass your toggleHidden() function as prop to AddTasktBox component. 您可以将toggleHidden()函数作为道具传递给AddTasktBox组件。

So your close button in AddTaskBox will be like 所以您在AddTaskBox中的关闭按钮会像

...
<button onClick={this.props.handleClick}> x </button>
...

You can pass it like 你可以像这样通过

{this.state.isHidden && <AddTaskBox handleClick={this.toggleHidden.bind(this)} />}

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

相关问题 如何使用reactjs中的按钮隐藏组件? - How to hide a component using button in reactjs? 如何使用 Typescript 将 object 传递给 ReactJS 中的自定义子组件? - How to pass an object to a custom child component in ReactJS using Typescript? 如何使用 jQuery 删除整个基于 div 的单击子项? - How to remove the whole div based clicked child using jQuery? 单击 ReactJS 中的按钮时添加和删除输入组件 - Add and Remove input component on click of a Button in ReactJS 模态在子组件中,按钮在父组件中如何关闭 reactjs 中的模态 - Modal is in child component where the button is in parent component how to close the modal in reactjs 在 ReactJS 中按下按钮时,如何使兄弟组件的子组件刷新? - How to make a child component of a sibling component refresh when a button is pressed in ReactJS? 父组件中的Reactjs按钮可在子组件中提交Redux表单 - Reactjs button in Parent component which submits Redux Form in Child Component Reactjs:从另一个组件单击按钮后未显示子组件 - Reactjs: child component not showing after clicking button from another component 如何在reactjs中使用高阶组件添加按钮 - How to add a button using Higher Order Component in reactjs reactjs-如何将数据从子组件转换为道具中传递的按钮的onclick? - reactjs - how to get data from a child component into onclick of a button passed in props?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM