简体   繁体   English

React-DOM自动触发onClick处理程序

[英]React-DOM automatically triggering onClick handler

I have a react-modal which shows some filters. 我有一个反应模式 ,显示了一些过滤器。 It accepts an isOpen prop which hides or shows the Modal as per the value. 它接受一个isOpen道具,该道具根据值隐藏或显示Modal。

The Modal opens successfully when clicked on 'Apply' button, but does not close on clicking 'Cancel'. 单击“应用”按钮时, Modal成功打开,但单击“取消”时Modal没有关闭。

//Click Handlers in the parent component (filters.js)
openAllFilters = () => {
  this.setState({ showAllFilters: true });
};

closeAllFilters = () => {
  this.setState({ showAllFilters: false }, () =>
    console.log("state ", this.state.showAllFilters)
  ); // logs true!);
};

<AllFilters isOpen={this.state.showAllFilters}
    closeAllFilters={this.closeAllFilters}
    onRequestClose={this.closeAllFilters}
    renderAbcFilter={this.renderAbcFilter}
    renderDefFilter={this.renderDefFilter}
    renderXyzFilter={this.renderXyzFilter}
/>

The Modal component (AllFilters.js): 模态组件(AllFilters.js):

import React from 'react';
import Modal from 'react-modal';

const RenderFilter = props => {
    return <div className="filter-wrapper">
        <h3 className="filter-title">{props.title}</h3>
        {props.renderFunction()}
    </div>;
}

const AllFilters = (props) => {
    const modalStyles = {
        overlay: {
            zIndex: 200,
        },
        content: {
            top: '0',
            left: '0',
            right: '0',
            bottom: '0',
            padding: '0',
        }
    };

    return (
        <Modal isOpen={props.isOpen}
            style={modalStyles}
            onRequestClose={props.closeAllFilters}>
            <div className="all-filters-container">
                <RenderFilter title='ABC' renderFunction={() => props.renderAbcFilter(false)} />
                <RenderFilter title='XYZ' renderFunction={() => props.renderXyzFilter(false)} />
                <RenderFilter title='DEF' renderFunction={() => props.renderDefFilter(false)} />
            </div>
            <div className="all-filters-footer">
                <button className="button button-secondary filter-cancel-btn " onClick={props.closeAllFilters}>CANCEL</button>
        </Modal >
    )
}

export default AllFilters;

I can't figure out why state is not updated to false when closeAllFilters is called? 我不知道为什么在closeAllFiltersstate没有更新为false EDIT: closeAllFilters does get called when 'Cancel' button is clicked. 编辑:单击“取消”按钮时,不会调用closeAllFilters And console.log(this.state.showAllFilters) outputs true !. 并且console.log(this.state.showAllFilters)输出true !。 Even though this is the setState callback! 即使这是setState回调!

EDIT2 : I figured that openAllfilters is somehow automatically getting called after closeAllFilters is invoked. EDIT2:我想, openAllfilters之后莫名其妙地自动获取调用closeAllFilters被调用。 Thus it sets showAllFilters back to true and the modal remains open. 因此,它将showAllFilters设置为true ,并且模式保持打开状态。 Have updated the question title to better reflect the issue. 更新了问题标题以更好地反映问题。 The stack trace for the same is as follows: 相同的堆栈跟踪如下:

openAllFilters (filters.js#382)
callCallback (react-dom.development.js#149)
invokeGuardedCallbackDev (react-dom.development.js#199)
invokeGuardedCallback (react-dom.development.js#256)
invokeGuardedCallbackAndCatchFirstError (react-dom.development.js#270)
executeDispatch (react-dom.development.js#561)
executeDispatchesInOrder (react-dom.development.js#580)
executeDispatchesAndRelease (react-dom.development.js#680)
executeDispatchesAndReleaseTopLevel (react-dom.development.js#688)
forEachAccumulated (react-dom.development.js#662)
runEventsInBatch (react-dom.development.js#816)
runExtractedEventsInBatch (react-dom.development.js#824)
handleTopLevel (react-dom.development.js#4826)
batchedUpdates$1 (react-dom.development.js#20439)
batchedUpdates (react-dom.development.js#2151)
dispatchEvent (react-dom.development.js#4905)
1 (react-dom.development.js#20490)
unstable_runWithPriority (scheduler.development.js#255)
interactiveUpdates$1 (react-dom.development.js#20489)
interactiveUpdates (react-dom.development.js#2170)
dispatchInteractiveEvent (react-dom.development.js#4882)

From above call stack, it seems that React is somehow triggering openAllFilters . 从上面的调用堆栈来看,React似乎以某种方式触发了openAllFilters I went through these function calls one by one, but still can't figure out why it's happening. 我一个接一个地经历了这些函数调用,但是仍然不知道为什么会发生这种情况。 Maybe someone who understands React source code well might be able to offer some insight.. 也许某个对React源代码非常了解的人可能能够提供一些见解。

So after a lot of debugging and head scratching, I finally figured out what the issue was. 因此,在进行大量调试和抓挠之后,我终于弄清了问题所在。 openAllFilters was getting invoked after call to closeAllFilters due to event propagation. openAllFilters调用后得到调用closeAllFilters由于事件传播。 The event is triggered by clicking anywhere on the modal. 通过单击模式上的任意位置来触发事件。 I didn't find any prop to disable this behaviour, so maybe it's a bug of react-modal . 我没有找到任何禁用此行为的道具,所以也许这是react-modal的错误。 So the only fix required was to add e.stopPropagation() ! 因此,唯一需要解决的就是添加e.stopPropagation()

So the changed method becomes: 因此,更改后的方法变为:

closeAllFilters = (e) => {
    this.setState({ showAllFilters: false });
    e.stopPropagation();
}

Thanks for all the help though! 谢谢所有的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM