React-bootstrap模态对属性变化不反应

发布于 2025-02-12 18:16:39 字数 1474 浏览 3 评论 0原文

我正在使用React V18.1,React-Bootstrap v2.4。我有一个模态组件,试图在按钮上显示。模态组件非常简单:

class AdjustmentModal extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            'show': this.props.show
        };

        this.handleClose = this.handleClose.bind(this);
    }

    handleClose() {
        this.setState({ show: false })
    }

    render() {
        return (
            <Modal show={this.state.show} onHide={this.handleClose}>
                [ ... Modal Content Here ... ]
            </Modal>
        );
    }
}

export default AdjustmentModal;

如您所见,我将模式的show属性绑定到状态中的show的值。

然后,在我要显示模式的组件中,我有以下内容:

// Within render() ...
<AdjustmentModal 
    show={this.state.showAdjustment}
    partNo={this.state.partNo}
    onHandQty={this.state.onHandQty}
/>
// Futher on in the code, display the modal on click:
<Button className="icon" onClick={this.handleDisplayAdjustment}>
    <i className="bi bi-pencil-square"></i>
</Button>

andledisplayAdjustment

handleDisplayAdjustment(event) {
    event.preventDefault();
    this.setState({
        showAdjustment : true
    });
}

现在,尽管值show> showdjustment在父母中,模态不显示。

我可以设置&lt; modal show = {this.props.show} .../&gt;而不是阅读,因此,如果阅读,则无法再次关闭模态来自道具而不是状态。

I am using React v18.1, react-bootstrap v2.4. I have a Modal component I am trying to get to display upon a button press. The modal component is quite simple:

class AdjustmentModal extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            'show': this.props.show
        };

        this.handleClose = this.handleClose.bind(this);
    }

    handleClose() {
        this.setState({ show: false })
    }

    render() {
        return (
            <Modal show={this.state.show} onHide={this.handleClose}>
                [ ... Modal Content Here ... ]
            </Modal>
        );
    }
}

export default AdjustmentModal;

As you can see, I bind the modal's show property to the value of show in state.

Then, in the component in which I want to display my modal, I have the following:

// Within render() ...
<AdjustmentModal 
    show={this.state.showAdjustment}
    partNo={this.state.partNo}
    onHandQty={this.state.onHandQty}
/>
// Futher on in the code, display the modal on click:
<Button className="icon" onClick={this.handleDisplayAdjustment}>
    <i className="bi bi-pencil-square"></i>
</Button>

handleDisplayAdjustment :

handleDisplayAdjustment(event) {
    event.preventDefault();
    this.setState({
        showAdjustment : true
    });
}

Now, despite the value showAdjustment in the parent component changing to true, the modal doesn't display.

I could set the <Modal show={this.props.show} .../> instead, but props are read-only, so there is no way to close the modal again if reading from props rather than state.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尴尬癌患者 2025-02-19 18:16:39

您可以使用道具,如果要关闭,这是一种更好的处理方法是模态组件和模态的子组件将获得更新的值,该值将是错误的。以下是如何实现这一目标的代码。

closeModal() {
  this.setState({
  showAdjustment: false
})
}

// Within render() ...
<AdjustmentModal 
    show={this.state.showAdjustment}
    partNo={this.state.partNo}
    onHandQty={this.state.onHandQty}
    onClose={this.closeModal.bind(this)}
/>
// Futher on in the code, display the modal on click:
<Button className="icon" onClick={this.handleDisplayAdjustment}>
    <i className="bi bi-pencil-square"></i>
</Button>

对于儿童组件

    class AdjustmentModal extends React.Component {

    handleClose() {
        this.props.onClose()
    }

    render() {
        return (
            <Modal show={this.props.show} onHide={this.handleClose}>
                [ ... Modal Content Here ... ]
            </Modal>
        );
    }
}

export default AdjustmentModal;

编辑:解释方法
这将使您的模态组件成为由父母控制的受控组件,也将道具作为子组件内部的状态更新不正确,这可能会产生潜在的错误。

You can use props, which is a better way to handle this if you want to close it then pass a method from the parent which when called update the state in the parent to false and due state update the parent component will re render and though the child component that is the modal component and the Modal will get the updated value which will be false. below is the code on how you can achieve that.

closeModal() {
  this.setState({
  showAdjustment: false
})
}

// Within render() ...
<AdjustmentModal 
    show={this.state.showAdjustment}
    partNo={this.state.partNo}
    onHandQty={this.state.onHandQty}
    onClose={this.closeModal.bind(this)}
/>
// Futher on in the code, display the modal on click:
<Button className="icon" onClick={this.handleDisplayAdjustment}>
    <i className="bi bi-pencil-square"></i>
</Button>

For the child component

    class AdjustmentModal extends React.Component {

    handleClose() {
        this.props.onClose()
    }

    render() {
        return (
            <Modal show={this.props.show} onHide={this.handleClose}>
                [ ... Modal Content Here ... ]
            </Modal>
        );
    }
}

export default AdjustmentModal;

EDIT: Explaining the approach
This will make your Modal component a Controlled component that is controlled by Parent, also updating props as a state inside the child component is not the right way, which may create potential bugs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文