gpt4 book ai didi

javascript - 如何在 x 秒内关闭模态?

转载 作者:行者123 更新时间:2023-11-30 19:55:37 24 4
gpt4 key购买 nike

我正在使用 ReactJS 和 Bootstrap 模式。我可以很好地打开模式,但我希望它在 3 秒后关闭。

我尝试了 setTimeout,如下所示,但它没有关闭。我为 setTimeout 提供了 handleClose 的回调,但在控制台记录后,我可以看到没有调用 handleClose。

这是 ItemDetailView 组件:

    import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Card, CardImg, CardText, CardBody,
CardTitle, CardSubtitle } from 'reactstrap';
import { addToCart } from '../actions/addToCartAction';
import './ItemDetailView.css';
import ItemAddedModal from './ItemAddedModal';


class ItemDetailView extends Component {
constructor(props) {
super(props);

this.state = {
modalOpen: false
}
// this.toggle = this.toggle.bind(this);
};

// toggle() {
// this.setState({
// modalOpen: !this.state.modalOpen
// });
// };

handleOpen = () => {
console.log("Cart Open", this.state.modalOpen);
this.setState({
modalOpen: true
},() => {setTimeout(this.handleClose(), 3000)});
// setTimeout(this.handleClose(), 3000);
};

handleClose = () => {
this.setState({
modalOpen: false
});
console.log('handleClose fired!')
};

addToCartHandler = () => {
this.props.addToCart(this.props.card);
console.log('addToCart++', this.props.quantity);
this.handleOpen()
// this.setState({
// modalOpen: true
// });
};

render() {
if (!this.props.title) {
return null;
}

return (
<div className="detail-view-wrapper">
<Card className="text-center detail-view-card">
{/* <CardImg top width="100%" src={"/" + this.props.img} alt={this.props.title} /> */}
<CardImg className="detail-view-img" top width="100%" src={"/" + this.props.img} alt={this.props.title} />
<CardBody>
<CardTitle className={"card-title"}>{this.props.title}</CardTitle>
<CardSubtitle>${this.props.price}</CardSubtitle>
<CardText>{this.props.description}</CardText>
{/* <SvgIcon className="cart-icon" onClick={() => this.addToCartHandler()} >
<AddShoppingCart />
</SvgIcon> */}
<button className= "add-to-cart-button" onClick={() => this.addToCartHandler()}>Add To Cart</button>
</CardBody>
</Card>
<ItemAddedModal open={this.state.modalOpen} toggle={this.toggle} />
</div>
);
}
}

const mapStateToProps = state => {
if (!state.data.cardData) {
return {
title: null,
img: null,
description: null,
price: null
}
}
const card = state.data.cardData[state.card.id]
return {
card: card,
title: card.title,
id: card.id,
img: card.img,
description: card.description,
price: card.price,
quantity: 0
};
}

export default connect(mapStateToProps, { addToCart })(ItemDetailView);

这是 ItemAddedModal:

import React from 'react';
import { Modal, ModalHeader } from 'reactstrap';
import './ItemAddedModal.css';

class ItemAddedModal extends React.Component {

render () {
return (
<div>
<Modal className="item-added-modal" isOpen={this.props.open} toggle={this.props.toggle} className={this.props.className}>
<ModalHeader className="item-added-modal-header">
<p className="item-added-modal-p">Item Added To Cart</p>
</ModalHeader>
</Modal>
</div>
)
};
}
export default ItemAddedModal;

最佳答案

要在设置状态后执行操作,我们需要将回调传递给 setState。

 this.setState({
modalOpen: true
},()=>{
console.log(this.state.modalOpen);});

关于javascript - 如何在 x 秒内关闭模态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54033350/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com