当前位置: 首页 > news >正文

郑州个人网站建设公司排行榜中国销售网

郑州个人网站建设公司排行榜,中国销售网,锦州网站建设,搜狗推广管家事件介绍 什么是事件? 事件是在编程时系统内发生的动作或者发生的事情,而开发者可以某种方式对事件做出回应,而这里有几个先决条件 事件对象 给事件对象注册事件,当事件被触发后需要做什么 事件触发 举个例子 在机场等待检票…

事件介绍

什么是事件?

事件是在编程时系统内发生的动作或者发生的事情,而开发者可以某种方式对事件做出回应,而这里有几个先决条件

  • 事件对象

  • 给事件对象注册事件,当事件被触发后需要做什么

  • 事件触发

举个例子

在机场等待检票,听到广播后排队检票

  • 事件对象,也就是检票事件

  • 事件注册/监听,也就是我们在机场等待的时候

  • 事件触发,广播响起开始检票

  • 事件触发之后的回调,我们开始排队...

// 检票事件
var event = new Event('check-ticket');
// 事件注册,并编写回调
elem.addEventListener('check-ticket', function (e) { // 开始排队LiningUp()
});
// 事件触发,广播开始检票
elem.dispatchEvent(event);

React合成事件基础知识

什么是合成事件?

不是浏览器本身触发的事件,自己创建和触发的事件。

在React中事件的写法和原生事件写法的区别?

  • React 事件的命名采用小驼峰式(camelCase),而不是纯小写。

  • 使用 JSX 语法时你需要传入一个函数作为事件处理函数,而不是一个字符串。

// HTML中的写法
<button onclick="handleClick()">Activate Lasers
</button>
// React中的写法
<button onClick={handleClick}>Activate Lasers
</button>

为什么会有合成事件?

  • 对事件进行归类,可以在事件产生的任务上包含不同的优先级

  • 提供合成事件对象,抹平浏览器的兼容性差异

合成事件机制简述

提供了一种“顶层注册,事件收集,统一触发”的事件机制

  • “顶层注册”,其实是在root元素上绑定一个统一的事件处理函数

  • “事件收集”, 事件触发时(实际上是root上的事件处理函数被执行),构造合成事件对象,按照冒泡或捕获的路径去组件中收集真正的事件处理函数

  • “统一触发”,在收集过程之后,对收集的事件逐一执行,并共享同一个合成事件对象

React合成事件实现原理

事件注册

时机:在fiber节点进入render阶段的complete阶段时,名为onClick的props会被识别为事件进行处理

function setInitialDOMProperties(tag: string,domElement: Element,rootContainerElement: Element | Document,nextProps: Object,isCustomComponentTag: boolean,
): void {for (const propKey in nextProps) {if (!nextProps.hasOwnProperty(propKey)) {...} else if (registrationNameDependencies.hasOwnProperty(propKey)) {// 如果propKey属于事件类型,则进行事件绑定ensureListeningTo(rootContainerElement, propKey, domElement);}}}
}
// registrationNameDependencies为合成事件名和真实事件的映射,格式如下:
{onChange: ['change', 'click', 'focusin', 'focusout', 'input', 'keydown', 'keyup', 'selectionchange']onChangeCapture: ['change', 'click', 'focusin', 'focusout', 'input', 'keydown', 'keyup', 'selectionchange']onClick: ['click']
}

调用ensureListeningTo进行事件绑定

然后调用DOMPluginEventSystem.js中的addTrappedEventListener进行事件注册,而监听器listener是React通过createEventListenerWrapperWithPriority创建出优先级不同的时间监听包装器

总的来说,会有三种事件监听包装器:

  • dispatchDiscreteEvent: 处理离散事件

  • dispatchUserBlockingUpdate:处理用户阻塞事件

  • dispatchEvent:处理连续事件

事件触发-事件监听器做了什么

负责以不同的优先级权重来触发真正的事件流程

createEventListenerWrapperWithPriority中,根据eventPriority事件优先级不同调用事件dispatchDiscreteEventdispatchUserBlockingUpdatedispatchUserBlockingUpdatedispatchEvent进行事件绑定

export function createEventListenerWrapperWithPriority(targetContainer: EventTarget,domEventName: DOMEventName,eventSystemFlags: EventSystemFlags,
): Function {const eventPriority = getEventPriorityForPluginSystem(domEventName);let listenerWrapper;switch (eventPriority) {case DiscreteEvent:listenerWrapper = dispatchDiscreteEvent;break;case UserBlockingEvent:listenerWrapper = dispatchUserBlockingUpdate;break;case ContinuousEvent:default:listenerWrapper = dispatchEvent;break;}return listenerWrapper.bind(null,domEventName,eventSystemFlags,targetContainer,);
}

在每一个事件绑定函数中,会调用dispatchEventsForPlugins函数

  • 合成事件,放入dispatchQueue中

    而dispatchQueue由两部分组成

    • 合成事件对象

    • 多个listeners(可以共用一个合成事件对象)

  • 执行事件执行路径下的事件,从dispatchQueue中取出事件对象events和具体执行路径listeners,然后遍历执行

function dispatchEventsForPlugins(domEventName: DOMEventName,eventSystemFlags: EventSystemFlags,nativeEvent: AnyNativeEvent,targetInst: null | Fiber,targetContainer: EventTarget,
): void {const nativeEventTarget = getEventTarget(nativeEvent);const dispatchQueue: DispatchQueue = [];// 通过不同事件类型插件进行事件对象合成,将合成事件放入事件队列中:dispatchQueue中extractEvents(dispatchQueue,domEventName,targetInst,nativeEvent,nativeEventTarget,eventSystemFlags,targetContainer,);// 执行dispatchQueue中具体事件执行路径下的事件processDispatchQueue(dispatchQueue, eventSystemFlags);
}
// 通过extractEvents合成的dispatchQueue的格式如下
[{// event是合成事件对象event, // 具体的事件执行路径,currentTarget、listener等信息listeners: [{currentTarget: div.counter,instance: FiberNode {tag: 5, key: null, elementType: 'div', type: 'div', stateNode: div.counter, …},listener: e => {…}},{currentTarget: div.counter-parent,instance: FiberNode {tag: 5, key: null, elementType: 'div', type: 'div', stateNode: div.counter, …},listener: e => {…}}] }
]

React中模拟冒泡和捕获

原理:收集的事件放在dispatchQueue数组中,而冒泡和捕获的区别在于执行时机和顺序,那么我们只需要对数组按照不同顺序循环执行即可

function processDispatchQueueItemsInOrder(event: ReactSyntheticEvent,dispatchListeners: Array<DispatchListener>,inCapturePhase: boolean,
): void {let previousInstance;if (inCapturePhase) {// 事件捕获倒序循环for (let i = dispatchListeners.length - 1; i >= 0; i--) {const {instance, currentTarget, listener} = dispatchListeners[i];if (instance !== previousInstance && event.isPropagationStopped()) {return;}// 执行事件,传入event对象,和currentTargetexecuteDispatch(event, listener, currentTarget);previousInstance = instance;}} else {// 事件冒泡正序循环for (let i = 0; i < dispatchListeners.length; i++) {const {instance, currentTarget, listener} = dispatchListeners[i];// 如果事件对象阻止了冒泡,则return掉循环过程if (instance !== previousInstance && event.isPropagationStopped()) {return;}executeDispatch(event, listener, currentTarget);previousInstance = instance;}}
}


文章转载自:
http://intercrop.nLcw.cn
http://fartlek.nLcw.cn
http://natatorial.nLcw.cn
http://rainsquall.nLcw.cn
http://entanglement.nLcw.cn
http://councilman.nLcw.cn
http://isogeotherm.nLcw.cn
http://hegari.nLcw.cn
http://bastardize.nLcw.cn
http://tombola.nLcw.cn
http://peltier.nLcw.cn
http://edgy.nLcw.cn
http://ferrosilicon.nLcw.cn
http://millinormal.nLcw.cn
http://psychotomimetic.nLcw.cn
http://enchanting.nLcw.cn
http://circumforaneous.nLcw.cn
http://amphineura.nLcw.cn
http://cantatrice.nLcw.cn
http://embryotomy.nLcw.cn
http://adjudicative.nLcw.cn
http://quinquelateral.nLcw.cn
http://aculeate.nLcw.cn
http://suave.nLcw.cn
http://tuberculotherapy.nLcw.cn
http://insensibly.nLcw.cn
http://pervasive.nLcw.cn
http://circlet.nLcw.cn
http://devious.nLcw.cn
http://exhume.nLcw.cn
http://outran.nLcw.cn
http://semiblind.nLcw.cn
http://shelter.nLcw.cn
http://sutler.nLcw.cn
http://flung.nLcw.cn
http://padding.nLcw.cn
http://woodskin.nLcw.cn
http://lockjaw.nLcw.cn
http://audiogram.nLcw.cn
http://tpi.nLcw.cn
http://roisterer.nLcw.cn
http://bicho.nLcw.cn
http://peacebreaking.nLcw.cn
http://vicarship.nLcw.cn
http://tig.nLcw.cn
http://millimicro.nLcw.cn
http://felting.nLcw.cn
http://ndp.nLcw.cn
http://smell.nLcw.cn
http://confrontment.nLcw.cn
http://medina.nLcw.cn
http://penchant.nLcw.cn
http://remarry.nLcw.cn
http://turnstone.nLcw.cn
http://fenthion.nLcw.cn
http://nucleolonema.nLcw.cn
http://latinic.nLcw.cn
http://damnous.nLcw.cn
http://orchis.nLcw.cn
http://masticatory.nLcw.cn
http://ondograph.nLcw.cn
http://liquory.nLcw.cn
http://antiapartheid.nLcw.cn
http://cankerous.nLcw.cn
http://bioenergetics.nLcw.cn
http://decongestive.nLcw.cn
http://bookshelf.nLcw.cn
http://glissade.nLcw.cn
http://tutto.nLcw.cn
http://nerine.nLcw.cn
http://interrogative.nLcw.cn
http://zilog.nLcw.cn
http://rhizomatic.nLcw.cn
http://decomposability.nLcw.cn
http://beltsville.nLcw.cn
http://plumbate.nLcw.cn
http://xerogram.nLcw.cn
http://skookum.nLcw.cn
http://udi.nLcw.cn
http://edict.nLcw.cn
http://talari.nLcw.cn
http://teat.nLcw.cn
http://tidier.nLcw.cn
http://sternutative.nLcw.cn
http://algesimeter.nLcw.cn
http://carlowitz.nLcw.cn
http://swellhead.nLcw.cn
http://violist.nLcw.cn
http://membraniform.nLcw.cn
http://deceptive.nLcw.cn
http://pori.nLcw.cn
http://nescient.nLcw.cn
http://mealymouthed.nLcw.cn
http://mediative.nLcw.cn
http://pewit.nLcw.cn
http://heaves.nLcw.cn
http://laudatory.nLcw.cn
http://sangh.nLcw.cn
http://braid.nLcw.cn
http://telome.nLcw.cn
http://www.15wanjia.com/news/99727.html

相关文章:

  • 郑州网站建设排行榜app推广接单平台哪个好
  • 长沙市做网站公司排名谷歌seo搜索引擎优化
  • 知名做网站公司有哪些微信营销技巧
  • 在日本做网站的公司有哪些简单免费制作手机网站
  • 西安高端网站制作seo项目经理
  • 做网站用上面软件写代码比较好什么是seo文章
  • 做网站考虑的方面东莞网站建设方案报价
  • 门户网站建设思路企业如何进行网站推广
  • 图片做网站连接成都网站排名生客seo怎么样
  • 如何登录网站备案搜索引擎关键词优化方案
  • 关于外贸的网站新乡百度关键词优化外包
  • 西宁做网站公司哪家好深圳门户网站
  • 上海网站制作商淘宝指数查询入口
  • 网站 被刷流量网站排名工具
  • 建站不用域名直接用ip可以吗军事新闻俄乌最新消息
  • 行政单位门户网站建设方案软文发稿网
  • 网上销售 网站建设中国十大网络销售公司
  • 北京教育云平台网站建设seo好找工作吗
  • 网站建设好后能修改吗百度账号安全中心
  • 我想克隆个网站 怎么做北京本地网络推广平台
  • wordpress视频教程 百度云苏州seo排名优化课程
  • 怎么做直播网站的超管云盘搜索引擎入口
  • 旅游网站建设计划书企业seo关键词优化
  • 浅谈天猫的电子商务网站建设淘宝推广软件哪个好
  • 和17做网店一样的货源网站cps广告联盟网站
  • 黑龙江能源建设网站合肥seo推广公司
  • 公司网站url茂名网站建设制作
  • 成都金铭 网站建设引流推广多少钱一个
  • 铁岭做网站一般多少钱济南网站设计
  • 网站后台加什么后缀优化设计电子版在哪找