简体   繁体   English

没有突变观察者的Dom节点插入/移除

[英]Dom node insertion/removal without mutation observers

I'm building a Tour component in React whose purpose is to introduce the user to the web app's interface. 我正在React中构建一个Tour组件,其目的是将用户引入Web应用程序的界面。 Parts of the "Tour" involve validating the user's actions, (eg if the current step involves opening a modal, once the user does so, the "Tour" should progress otherwise it should show an error if the user tries to progress by clicking 'Next'). “游览”的部分涉及验证用户的动作,(例如,如果当前步骤涉及打开模态,一旦用户这样做,“游览”应该进展,否则如果用户通过点击'尝试进展则应该显示错误下一个')。

For this I need to detect changes in the DOM, (eg a modal being opened or a div with a specific class appearing) . 为此,我需要检测DOM中的更改(例如,打开模式或出现特定类的div) I've had some ideas about wiring up an 'onNext' function that progresses the tutorial once the user interacts with certain target elements (eg 'Open Modal' button), but this seems like a hack, I want to govern the progression of the tour only by the actual elements present in the DOM not by listening for clicks that will result in the necessary elements showing up eventually. 我有一些关于连接'onNext'功能的一些想法,一旦用户与某些目标元素交互(例如'Open Modal'按钮),就会推进教程,但这似乎是一个黑客攻击,我想控制进展仅通过DOM中存在的实际元素进行浏览,而不是通过监听将导致最终显示必要元素的点击。

One of the big constraints is avoiding MutationObservers in addition to usage of jQuery . 除了使用jQuery之外,其中一个重要的限制是避免使用MutationObservers With that said, I'm interested in hunches about how to validate the dom, how would one use pure javascript and the dom to determine the addition and removal of elements? 话虽如此,我对如何验证dom的预感感兴趣,如何使用纯javascript和dom来确定元素的添加和删除?

I think you're best served by implementing a Flux architecture to handle this. 我认为通过实施Flux架构来处理这个问题最好。 Redux is a good fit. Redux很适合。

Create a Redux Reducer for your tour progression. 为您的游览进程创建一个Redux Reducer。 The state of this reducer should be a key that corresponds to the current step of the tour that the user is within. 此减速器的状态应该是与用户所在的巡回的当前步骤相对应的键。

All components used in the tour should have access to this tour state as a prop. 游览中使用的所有组件都应该可以作为道具访问此游览状态。 Use this prop to determine functionality. 使用此prop可确定功能。 Ie for your example of a dialog that must be opened, the code might look like this, within a relevant component; 即,对于必须打开的对话框示例,代码可能在相关组件中看起来像这样;

openModal(){
    if(this.props.tourStep == 'prompt_modal_open'){
        ActionCreator.progressTourStep();
    }
    // code for actually opening the modal goes here
},

someOtherAction(){
    if(this.props.tourStep == 'prompt_modal_open'){
        //Display error message here
    } else {
        //normal action result here
    }
}

When the user is not taking the tour, simply set tourStep in the reducer to undefined , and any tour related functionality will be turned off. 当用户没有参加巡视时,只需将tourStep中的tourStep设置为undefined ,任何巡视相关功能都将被关闭。

Alternately, if you want to keep your components clean and "dumb", you can put this logic directly into the action creator with the help of Redux-Thunk; 或者,如果你想让你的组件保持干净和“哑”,你可以在Redux-Thunk的帮助下将这个逻辑直接放到动作创建器中;

ActionCreator.openModal = function(){
    return function(dispatch, getState){
        var state = getState();
        if(state.tourStep == 'prompt_modal_open'){
            dispatch({type: 'progress_tour_step'});
        }
        dispatch({type: 'open_modal'});
    }
}

ActionCreator.someOtherAction = function(){
    return function(dispatch, getState){
        var state = getState();
        if(state.tourStep != undefined){
            dispatch({type: 'show_error'});
        } else {
            dispatch({type: 'some_other_action_type'});
        }
    }
}

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

相关问题 如何在不使用DOM突变事件的情况下检测AJAX节点插入? - How can I detect AJAX node insertion without using DOM mutation events? 具有变异观察者的黑名单DOM元素 - Blacklist DOM elements with Mutation Observers DOM Mutation Observers比DOM Mutation事件慢吗? - Are DOM Mutation Observers slower than DOM Mutation Events? IE10是否支持DOM4突变观察者? - Does IE10 support DOM4 Mutation Observers? 如何使用Modernizr.js测试DOM4 Mutation Observers? - How to test for DOM4 Mutation Observers with Modernizr.js? 转储整个DOM树(包括事件监听器和变异观察器) - Dump the whole DOM tree (including event listeners and mutation observers) DOM Mutation Observers的跨浏览器支持状态是什么? - What's the state of cross-browser support for DOM Mutation Observers? 在没有轮询或突变观察者的情况下检测DIV高度的变化 - Detect when a DIV's height changes without polling or mutation observers Mutation Observer无法检测元素的dom移除 - Mutation Observer fails to detect element's dom removal 对DOM的变化做出反应:Mutation Observers,window.addEventListener(“onresize”,callback)还是CSS替代品? - Reacting to changes in the DOM: Mutation Observers, window.addEventListener(“onresize”, callback) or CSS alternatives?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM