【问题标题】:Close button isn't working for react-bootstrap modal component关闭按钮不适用于 react-bootstrap 模态组件
【发布时间】:2020-11-29 07:37:44
【问题描述】:

我正在尝试使用 react-bootstrap 模式和反应状态显示另一个组件。我正在遵循 react-bootstrap 模式代码并尝试以我自己的方式自定义状态。我能够在不使用 React.useStae 的情况下创建打开模式的状态。但问题是当我单击关闭按钮时模式没有关闭。那么,我该如何解决这个状态问题呢?

这是我正在关注的 react-bootstrap 模式代码:

这是我试图在模态中打开另一个组件的代码。但关闭 btn 不起作用:

import React, { Component, useState } from "react";
import { Link } from "react-router-dom";
//COMPONENTS
import { Container, Row, Col, Modal, Button } from "react-bootstrap";
import FeatureCard from "../../widgets/ShoppingCards/FeatureCard/FeatureCard";
import AllDressHeader from "../../widgets/AllDressHeader/AllDressHeader";
import ProductCard from "../../widgets/ShoppingCards/ProductCard/ProductCard";
import Slider from "react-slick";

//Images
import ShirtHeader from "../../../assets/Shirts/header-new.jpg";
import img from "../../../assets/Shirts/A-unsplash.jpg";
import img2 from "../../../assets/Shirts/ocean-shirt.jpg";
import img3 from "../../../assets/Shirts/L-unsplash.jpg";
import img4 from "../../../assets/Shirts/denim.jpg";
import img5 from "../../../assets/Shirts/R-unsplash.jpg";
import img6 from "../../../assets/Shirts/P-denim.jpg";
import img7 from "../../../assets/Shirts/Q-unsplash.jpg";
import img8 from "../../../assets/Shirts/Y-unsplash.jpg";
import img9 from "../../../assets/Shirts/Mens-Popover.jpg";
import img10 from "../../../assets/Shirts/blur-shirt.jpg";
import shirts from "../../../assets/Shirts/A-unsplash.jpg";

class Shirts extends Component {
  state = {
    whichComponentToShow: "FeatureCard",
    show: false,
    setShow: false,
  };

  onClick = () => {
    this.setState({ whichComponentToShow: "ProductCard", show: true });
  };

  handleClose = () => {this.setState({setShow:false})};
  handleShow = () =>{this.setState({setShow:true})};

  render() {
    const featureCard = (
      <FeatureCard
        OnClick={this.onClick}
        image={img}
        title="Sky Blue"
        price="9$"
      />
    );
    const productCard = (
      <ProductCard image={shirts} title="Sky Blue" price="9$" add="shirt" />
    );

    //WHICH COMPONENT TO SHOW
    const ShowComponent = () => {
      if (this.state.whichComponentToShow === "FeatureCard") {
        return <div>{featureCard}</div>;
      } else if (this.state.whichComponentToShow === "ProductCard") {
        return (
          <div>
            <Modal
              show={this.state.show}
              onHide={this.handleClose}
              centered
              size="xl"
            >
              <Modal.Header closeButton></Modal.Header>
              <Modal.Body>
                <Container fluid>
                  <Row>
                    <Col lg={12}> {productCard}</Col>
                  </Row>
                </Container>
              </Modal.Body>
              <Modal.Footer>
                <Button variant="secondary" onClick={this.handleClose}>
                  Close
                </Button>
              </Modal.Footer>
            </Modal>
          </div>
        );
      }
    };

   
    return (
      <div className="Shirts" style={{ margin: "3rem 0rem" }}>
        <div>
          <AllDressHeader
            Image={ShirtHeader}
            h1="SHIRTS FOR MEN"
            para="Id adipisicing elit adipisicing Lorem. Laborum deserunt laboris ex aliqua deserunt ipsum irure culpa pariatur in esse esse quis. Laboris incididunt enim velit reprehenderit irure. Do est deserunt sint."
            h2="CHOOSE YOUR FAVOURITE SHIRT NOW"
          />
        </div>
        <Container>
          <Row>
            <div>{ShowComponent()}</div>

            <Col>
              <FeatureCard image={img2} title="Beach Light" price="25.50$" />
            </Col>
            <Col>
              <div>
                <FeatureCard
                  image={img3}
                  title="Official Formal"
                  price="9.99$"
                />
              </div>
            </Col>
            <Col>
              <div>
                <FeatureCard image={img4} title="Denim" price="19$" />
              </div>
            </Col>
          </Row>
          <Row>
            <Col>
              <div>
                <FeatureCard image={img5} title="Red Black" price="12$" />
              </div>
            </Col>
            <Col>
              <div>
                <FeatureCard
                  image={img6}
                  title="Blue White Denim"
                  price="56$"
                />
              </div>
            </Col>
            <Col>
              <div>
                <FeatureCard
                  image={img7}
                  title="White Long Sleeve"
                  price="8$"
                />
              </div>
            </Col>
            <Col>
              <div>
                <FeatureCard image={img8} title="Blue Dotted" price="9.50$" />
              </div>
            </Col>
          </Row>
        </Container>
      </div>
    );
  }
}

export default Shirts;

【问题讨论】:

  • 为什么使用setShow?您已经有show 来管理状态,在handleClose 中,您只需将其设置为falsehandleClose = () =&gt; {this.setState({show:false})};
  • 感谢工作。但现在又出现了一个问题。实际上,当我单击功能卡组件时,会弹出模式,但是当我关闭模式时,功能卡组件会从列表中删除,它不会显示在列表中。但我想在关闭后保留功能卡组件模态的。那么我该如何解决这个问题呢?谢谢

标签: javascript reactjs redux ecmascript-6 react-bootstrap


【解决方案1】:

您的模态引用 this.state.show,但您的 handleCLose/Show 函数正在设置 this.state.setShow。

您应该设置“显示”

  handleClose = () => {this.setState({show:false})};
  handleShow = () =>{this.setState({show:true})};

您正在查看的示例是一个功能组件,其中 setShow 是 useState 挂钩的突变方法。您正在使用一个基于类的组件,该组件使用 setState 来改变其状态。

【讨论】:

  • 感谢工作。但现在又出现了一个问题。当模式关闭时,组件 本身会从列表中删除,我连续有 4 个组件,但在我关闭模式后它显示 3。实际上,当我单击功能卡组件时,会弹出模式,但是当我关闭模式时,功能卡组件会从列表中删除,它不会显示在列表中。那么我该如何解决这个问题呢?谢谢
猜你喜欢
  • 1970-01-01
  • 2022-11-17
  • 1970-01-01
  • 2019-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-13
相关资源
最近更新 更多