reactjs - 如何在反应函数中关闭引导模式
问题描述
在我更新组件中调用 handleClose 函数的日期并且模式关闭后,我在我的反应组件中打开了 2 个模式。现在的问题是只有组件的主体正在消失,模态标题仍然显示。当我点击模态关闭按钮时,它消失了。我的问题是如何从反应函数中关闭引导模式?
export default class Index extends Component {
constructor() {
super();
this.state = {
services: [],
showHideNew: false,
showHideEdit: false,
editId: null,
loaded: false
};
this.hideComponent = this.hideComponent.bind(this);
this.fetchService = this.fetchService.bind(this);
this.handelClose = this.handelClose.bind(this)
}
componentDidMount() {
this.fetchService()
}
hideComponent(name) {
switch (name) {
case "showHideNew":
this.setState({ showHideNew: true });
this.setState({ showHideEdit: false });
break;
case "showHideEdit":
this.setState({ showHideEdit: true });
this.setState({ showHideNew: false });
break;
}
}
updateService(id) {
this.setState({editId: id})
this.hideComponent("showHideEdit")
}
fetchService = async () => {
const response = await fetch(`/api/services`)
const initialService = await response.json()
const services = initialService
this.setState({ services })
}
handelClose = () => {
console.log("close here")
this.setState({showHideEdit: false})
this.setState({showHideNew: false})
}
render() {
const { showHideNew, showHideEdit} = this.state;
return (
<>
<div>
<div class="col-md-12 align-self-center text-right">
<button onClick={() => this.hideComponent("showHideNew")} type="button" class="btn btn-grad" data-toggle="modal" data-target="#exampleModal5">
New Service
</button>
<small class="d-block mt-2"></small>
</div>
<div class="col-sm-12 mb-5">
<h5 class="text-center mb-4">Services</h5>
<div class="table-responsive-sm">
<table class="table table-hover">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Title</th>
<th scope="col">Tag</th>
<th scope="col">Edit</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
{this.state.services.map(service =>
<tr>
<th scope="row">{service.id}</th>
<td>{service.title}</td>
<td>{service.tag}</td>
<td><button data-toggle="modal" data-target="#exampleModal6" onClick={() => this.updateService(service.id)}>
<i class="ti-pencil"></i>
</button></td>
<td><button onClick={() => this.deleteService(service.id)}>
<i class="ti-trash"></i>
</button></td>
</tr>
)}
</tbody>
</table>
</div>
</div>
<div class="modal fade text-left" id="exampleModal5" tabindex="-1" role="dialog" aria-labelledby="exampleModal5" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">New Service</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
{showHideNew && <ServiceNew closeModal = {this.handelClose.bind(this)} getService ={this.fetchService.bind(this)} />}
</div>
</div>
</div>
</div>
<div class="modal fade text-left" id="exampleModal6" tabindex="-1" role="dialog" aria-labelledby="exampleModal6" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Edit User</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
{showHideEdit && <Edit getService ={this.fetchService.bind(this)} services = {this.state.services} editId = {this.state.editId} closeModal = {this.handelClose.bind(this)}/>}
</div>
</div>
</div>
</div>
</div>
</>
);
}
}
解决方案
推荐阅读
- clojure - 将地图列表“展平”为clojure中的单个地图?
- encoding - 带有标签均值的测试集目标编码
- java - 使用子类测试抽象类中的具体方法
- c# - Convert.ChangeType() 是否只返回对传递值的不同类型的引用?
- javascript - 为什么无法在@material-ui 中设置成功按钮的样式
- angular - 第一秒后可观察到的返回,他们有不同的超时
- apache-kafka - Kafka Streams - 在流线程开始处理之前恢复全局状态存储
- sql-server - SQL Server 允许您删除另一个过程所依赖的过程?
- firebase - 有什么方法可以在 Firebase Cloud Functions 中使用 Cordova 插件?
- swift - 对变量值感到困惑?