get listener
一、作用
二、获取监听器
备注
getFiberCurrentPropsFromNode()由 ReactDOMComponentTree#getFiberCurrentPropsFromNode 实现
/**
* @param {object} inst The instance, which is the source of events.
* 实例:事件的来源实例
* @param {string} registrationName Name of listener (e.g. `onClick`).
* registrationName 监听器的名称(例如 `onClick`)
* @return {?function} The stored callback.
* 存储的回调
*/
export default function getListener(
inst: Fiber,
registrationName: string,
): Function | null {
const stateNode = inst.stateNode;
if (stateNode === null) {
// Work in progress (ex: onload events in incremental mode).
// 正在进行中(例如:增量模式下的 onload 事件)。
return null;
}
const props = getFiberCurrentPropsFromNode(stateNode);
if (props === null) {
// Work in progress.
// 正在进行中。
return null;
}
const listener = props[registrationName];
if (shouldPreventMouseEvent(registrationName, inst.type, props)) {
return null;
}
if (listener && typeof listener !== 'function') {
throw new Error(
`Expected \`${registrationName}\` listener to be a function, instead got a value of \`${typeof listener}\` type.`,
);
}
return listener;
}
三、工具
1. 是否交互
function isInteractive(tag: string): boolean {
return (
tag === 'button' ||
tag === 'input' ||
tag === 'select' ||
tag === 'textarea'
);
}
2. 应该阻止鼠标事件
function shouldPreventMouseEvent(
name: string,
type: string,
props: Props,
): boolean {
switch (name) {
case 'onClick':
case 'onClickCapture':
case 'onDoubleClick':
case 'onDoubleClickCapture':
case 'onMouseDown':
case 'onMouseDownCapture':
case 'onMouseMove':
case 'onMouseMoveCapture':
case 'onMouseUp':
case 'onMouseUpCapture':
case 'onMouseEnter':
return !!(props.disabled && isInteractive(type));
default:
return false;
}
}