【问题标题】:react: recalling the dialog via a state changereact: 通过状态改变调用对话框
【发布时间】:2021-05-03 18:33:23
【问题描述】:

我有一个下拉列表和一个对话框,当下拉列表中的项目(类dialogselected)被选中时显示

代码:

// function is triggered when an item in the drop-down list is selected
handleSelect = (eventKey: any, event: any) => {

    this.setState({
        selected_id:        eventKey,
        is_activated:       event.target.classList.contains('dialogselected')
    });
}

// display a drop-down list and a modal dialog
render() {
    return (
        <>
            <Dropdown>
                <Dropdown.Menu>
                    <Dropdown.Item 
                        onSelect = {this.handleSelect}
                    >...</Dropdown.Item>
                </Dropdown.Menu>
            </Dropdown>
            {this.state.is_activated ? <MyDialog /> : null}
        </>
    );
}

When an item with the class dialogselected is selected, the dialog is displayed.

如果我关闭对话框并再次选择相同的项目,则对话框将不会再次显示因为状态没有改变,即is_activatedtrue 并保持不变。 p>

您能建议如何正确解决这种情况吗?

即该对话框需要重复调​​用,即我们需要以某种方式让 geast 知道状态已被拉出

【问题讨论】:

    标签: reactjs state react-bootstrap setstate


    【解决方案1】:

    如果你能传递更多的代码就更好了,这样我们就能看到更大的图景了。

    我最初的猜测是event.target.classList.contains('dialogselected') 有问题,也许你没有正确地在元素上切换它。

    【讨论】:

    • 我检查了-从下拉列表中选择一个项目是正确的,设置状态是正确的,但是由于状态没有改变,所以react没有响应设置状态-这就是问题.也许你需要再次直接调用render方法?当状态this.state.is_activated 更改时会显示该对话框,如果您在下拉列表中选择相同的项目,它不会更改,如果您先选择任何其他项目,然后选择所需的项目,那么一切正常
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-14
    • 1970-01-01
    • 2021-03-15
    • 2020-09-11
    相关资源
    最近更新 更多