跳到主要内容

React DOM float

一、作用

二、预取DNS

如果参数不是字符串,则使用内置方法解析,否则则忽略。

备注
export function prefetchDNS(href: string) {
if (__DEV__) {
if (typeof href !== 'string' || !href) {
console.error(
'ReactDOM.prefetchDNS(): Expected the `href` argument (first) to be a non-empty string but encountered %s instead.',
getValueDescriptorExpectingObjectForWarning(href),
);
} else if (arguments.length > 1) {
const options = arguments[1];
if (
typeof options === 'object' &&
options.hasOwnProperty('crossOrigin')
) {
console.error(
'ReactDOM.prefetchDNS(): Expected only one argument, `href`, but encountered %s as a second argument instead. This argument is reserved for future options and is currently disallowed. It looks like the you are attempting to set a crossOrigin property for this DNS lookup hint. Browsers do not perform DNS queries using CORS and setting this attribute on the resource hint has no effect. Try calling ReactDOM.prefetchDNS() with just a single string argument, `href`.',
getValueDescriptorExpectingEnumForWarning(options),
);
} else {
console.error(
'ReactDOM.prefetchDNS(): Expected only one argument, `href`, but encountered %s as a second argument instead. This argument is reserved for future options and is currently disallowed. Try calling ReactDOM.prefetchDNS() with just a single string argument, `href`.',
getValueDescriptorExpectingEnumForWarning(options),
);
}
}
}
if (typeof href === 'string') {
ReactDOMSharedInternals.d /* ReactDOMCurrentDispatcher */
.D(/* prefetchDNS */ href);
}
// We don't error because preconnect needs to be resilient to being called in a variety of scopes
// and the runtime may not be capable of responding. The function is optimistic and not critical
// so we favor silent bailout over warning or erroring.
// 我们不会报错,因为 preconnect 需要能够在各种范围内调用而保持稳健,并且运行时可能无法响应。
// 该函数是乐观的,并且不是关键功能,因此我们更倾向于静默中止而不是发出警告或报错。
}

三、预链接

与预取 DNS 同理,只有在链接 href 为字符串时解析,否则忽略。

备注
export function preconnect(href: string, options?: ?PreconnectOptions) {
if (__DEV__) {
if (typeof href !== 'string' || !href) {
console.error(
'ReactDOM.preconnect(): Expected the `href` argument (first) to be a non-empty string but encountered %s instead.',
getValueDescriptorExpectingObjectForWarning(href),
);
} else if (options != null && typeof options !== 'object') {
console.error(
'ReactDOM.preconnect(): Expected the `options` argument (second) to be an object but encountered %s instead. The only supported option at this time is `crossOrigin` which accepts a string.',
getValueDescriptorExpectingEnumForWarning(options),
);
} else if (options != null && typeof options.crossOrigin !== 'string') {
console.error(
'ReactDOM.preconnect(): Expected the `crossOrigin` option (second argument) to be a string but encountered %s instead. Try removing this option or passing a string value instead.',
getValueDescriptorExpectingObjectForWarning(options.crossOrigin),
);
}
}
if (typeof href === 'string') {
const crossOrigin = options
? getCrossOriginString(options.crossOrigin)
: null;
ReactDOMSharedInternals.d /* ReactDOMCurrentDispatcher */
.C(/* preconnect */ href, crossOrigin);
}
// We don't error because preconnect needs to be resilient to being called in a variety of scopes
// and the runtime may not be capable of responding. The function is optimistic and not critical
// so we favor silent bailout over warning or erroring.
// 我们不会报错,因为 preconnect 需要能够在各种范围内调用而保持稳健,并且运行时可能无法响应。
// 这个函数是乐观的,并且不是关键的。因此我们更倾向于静默退出,而不是发出警告或报错。
}

四、预下载

备注
export function preload(href: string, options: PreloadOptions) {
if (__DEV__) {
let encountered = '';
if (typeof href !== 'string' || !href) {
encountered += ` The \`href\` argument encountered was ${getValueDescriptorExpectingObjectForWarning(
href,
)}.`;
}
if (options == null || typeof options !== 'object') {
encountered += ` The \`options\` argument encountered was ${getValueDescriptorExpectingObjectForWarning(
options,
)}.`;
} else if (typeof options.as !== 'string' || !options.as) {
encountered += ` The \`as\` option encountered was ${getValueDescriptorExpectingObjectForWarning(
options.as,
)}.`;
}
if (encountered) {
console.error(
'ReactDOM.preload(): Expected two arguments, a non-empty `href` string and an `options` object with an `as` property valid for a `<link rel="preload" as="..." />` tag.%s',
encountered,
);
}
}
if (
typeof href === 'string' &&
// We check existence because we cannot enforce this function is actually called with the stated type
// 我们检查存在性,因为我们无法确保这个函数实际上是以指定的类型调用的
typeof options === 'object' &&
options !== null &&
typeof options.as === 'string'
) {
const as = options.as;
const crossOrigin = getCrossOriginStringAs(as, options.crossOrigin);
ReactDOMSharedInternals.d /* ReactDOMCurrentDispatcher */
.L(/* preload */ href, as, {
crossOrigin,
integrity:
typeof options.integrity === 'string' ? options.integrity : undefined,
nonce: typeof options.nonce === 'string' ? options.nonce : undefined,
type: typeof options.type === 'string' ? options.type : undefined,
fetchPriority:
typeof options.fetchPriority === 'string'
? options.fetchPriority
: undefined,
referrerPolicy:
typeof options.referrerPolicy === 'string'
? options.referrerPolicy
: undefined,
imageSrcSet:
typeof options.imageSrcSet === 'string'
? options.imageSrcSet
: undefined,
imageSizes:
typeof options.imageSizes === 'string'
? options.imageSizes
: undefined,
media: typeof options.media === 'string' ? options.media : undefined,
});
}
// We don't error because preload needs to be resilient to being called in a variety of scopes
// and the runtime may not be capable of responding. The function is optimistic and not critical
// so we favor silent bailout over warning or erroring.
//
// 我们不会报错,因为 preload 需要能够在各种作用域中被调用
// 并且运行时可能无法做出响应。该函数是乐观的且非关键的
// 因此我们更倾向于静默终止,而不是发出警告或报错。
}

五、预下载模块

备注
export function preloadModule(href: string, options?: ?PreloadModuleOptions) {
if (__DEV__) {
let encountered = '';
if (typeof href !== 'string' || !href) {
encountered += ` The \`href\` argument encountered was ${getValueDescriptorExpectingObjectForWarning(
href,
)}.`;
}
if (options !== undefined && typeof options !== 'object') {
encountered += ` The \`options\` argument encountered was ${getValueDescriptorExpectingObjectForWarning(
options,
)}.`;
} else if (options && 'as' in options && typeof options.as !== 'string') {
encountered += ` The \`as\` option encountered was ${getValueDescriptorExpectingObjectForWarning(
options.as,
)}.`;
}
if (encountered) {
console.error(
'ReactDOM.preloadModule(): Expected two arguments, a non-empty `href` string and, optionally, an `options` object with an `as` property valid for a `<link rel="modulepreload" as="..." />` tag.%s',
encountered,
);
}
}
if (typeof href === 'string') {
if (options) {
const crossOrigin = getCrossOriginStringAs(
options.as,
options.crossOrigin,
);
ReactDOMSharedInternals.d /* ReactDOMCurrentDispatcher */
.m(/* preloadModule */ href, {
as:
typeof options.as === 'string' && options.as !== 'script'
? options.as
: undefined,
crossOrigin,
integrity:
typeof options.integrity === 'string'
? options.integrity
: undefined,
});
} else {
ReactDOMSharedInternals.d /* ReactDOMCurrentDispatcher */
.m(/* preloadModule */ href);
}
}
// We don't error because preload needs to be resilient to being called in a variety of scopes
// and the runtime may not be capable of responding. The function is optimistic and not critical
// so we favor silent bailout over warning or erroring.
//
// 我们不会报错,因为 preload 需要能够在各种作用域中被调用
// 并且运行时可能无法做出响应。该函数是乐观的且非关键的
// 因此我们更倾向于静默终止,而不是发出警告或报错。
}

六、预初始化

备注
export function preinit(href: string, options: PreinitOptions) {
if (__DEV__) {
if (typeof href !== 'string' || !href) {
console.error(
'ReactDOM.preinit(): Expected the `href` argument (first) to be a non-empty string but encountered %s instead.',
getValueDescriptorExpectingObjectForWarning(href),
);
} else if (options == null || typeof options !== 'object') {
console.error(
'ReactDOM.preinit(): Expected the `options` argument (second) to be an object with an `as` property describing the type of resource to be preinitialized but encountered %s instead.',
getValueDescriptorExpectingEnumForWarning(options),
);
} else if (options.as !== 'style' && options.as !== 'script') {
console.error(
'ReactDOM.preinit(): Expected the `as` property in the `options` argument (second) to contain a valid value describing the type of resource to be preinitialized but encountered %s instead. Valid values for `as` are "style" and "script".',
getValueDescriptorExpectingEnumForWarning(options.as),
);
}
}
if (typeof href === 'string' && options && typeof options.as === 'string') {
const as = options.as;
const crossOrigin = getCrossOriginStringAs(as, options.crossOrigin);
const integrity =
typeof options.integrity === 'string' ? options.integrity : undefined;
const fetchPriority =
typeof options.fetchPriority === 'string'
? options.fetchPriority
: undefined;
if (as === 'style') {
ReactDOMSharedInternals.d /* ReactDOMCurrentDispatcher */
.S(
/* preinitStyle */
href,
typeof options.precedence === 'string'
? options.precedence
: undefined,
{
crossOrigin,
integrity,
fetchPriority,
},
);
} else if (as === 'script') {
ReactDOMSharedInternals.d /* ReactDOMCurrentDispatcher */
.X(/* preinitScript */ href, {
crossOrigin,
integrity,
fetchPriority,
nonce: typeof options.nonce === 'string' ? options.nonce : undefined,
});
}
}
// We don't error because preinit needs to be resilient to being called in a variety of scopes
// and the runtime may not be capable of responding. The function is optimistic and not critical
// so we favor silent bailout over warning or erroring.
//
// 我们不会报错,因为 preinit 需要能够应对在各种作用域中被调用的情况
// 并且运行时可能无法响应。这个函数是乐观的,并且不是关键的
// 所以我们更倾向于静默退出,而不是发出警告或报错。
}

七、预初始化模块

备注
export function preinitModule(href: string, options?: ?PreinitModuleOptions) {
if (__DEV__) {
let encountered = '';
if (typeof href !== 'string' || !href) {
encountered += ` The \`href\` argument encountered was ${getValueDescriptorExpectingObjectForWarning(
href,
)}.`;
}
if (options !== undefined && typeof options !== 'object') {
encountered += ` The \`options\` argument encountered was ${getValueDescriptorExpectingObjectForWarning(
options,
)}.`;
} else if (options && 'as' in options && options.as !== 'script') {
encountered += ` The \`as\` option encountered was ${getValueDescriptorExpectingEnumForWarning(
options.as,
)}.`;
}
if (encountered) {
console.error(
'ReactDOM.preinitModule(): Expected up to two arguments, a non-empty `href` string and, optionally, an `options` object with a valid `as` property.%s',
encountered,
);
} else {
const as =
options && typeof options.as === 'string' ? options.as : 'script';
switch (as) {
case 'script': {
break;
}

// We have an invalid as type and need to warn
default: {
const typeOfAs = getValueDescriptorExpectingEnumForWarning(as);
console.error(
'ReactDOM.preinitModule(): Currently the only supported "as" type for this function is "script"' +
' but received "%s" instead. This warning was generated for `href` "%s". In the future other' +
' module types will be supported, aligning with the import-attributes proposal. Learn more here:' +
' (https://github.com/tc39/proposal-import-attributes)',
typeOfAs,
href,
);
}
}
}
}
if (typeof href === 'string') {
if (typeof options === 'object' && options !== null) {
if (options.as == null || options.as === 'script') {
const crossOrigin = getCrossOriginStringAs(
options.as,
options.crossOrigin,
);
ReactDOMSharedInternals.d /* ReactDOMCurrentDispatcher */
.M(/* preinitModuleScript */ href, {
crossOrigin,
integrity:
typeof options.integrity === 'string'
? options.integrity
: undefined,
nonce:
typeof options.nonce === 'string' ? options.nonce : undefined,
});
}
} else if (options == null) {
ReactDOMSharedInternals.d /* ReactDOMCurrentDispatcher */
.M(/* preinitModuleScript */ href);
}
}
// We don't error because preinit needs to be resilient to being called in a variety of scopes
// and the runtime may not be capable of responding. The function is optimistic and not critical
// so we favor silent bailout over warning or erroring.
//
// 我们不会报错,因为 preinit 需要能够应对在各种作用域中被调用的情况
// 并且运行时可能无法响应。这个函数是乐观的,并且不是关键的
// 所以我们更倾向于静默退出,而不是发出警告或报错。
}

八、工具

1. 获取值描述符时预期对象警告

function getValueDescriptorExpectingObjectForWarning(thing: any): string {
return thing === null
? '`null`'
: thing === undefined
? '`undefined`'
: thing === ''
? 'an empty string'
: `something with type "${typeof thing}"`;
}

2. 获取值描述符(预期枚举用于警告)

function getValueDescriptorExpectingEnumForWarning(thing: any): string {
return thing === null
? '`null`'
: thing === undefined
? '`undefined`'
: thing === ''
? 'an empty string'
: typeof thing === 'string'
? JSON.stringify(thing)
: typeof thing === 'number'
? '`' + thing + '`'
: `something with type "${typeof thing}"`;
}