[英]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应用,并且有这样的功能:
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.