/
githubmr
/
facebook-react
Обзор
Документация
Войти
/
githubmr
/
facebook-react
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
v16.10.2
packages/react/src/ReactLazy.js
69 строк
2 KB
Dan Abramov
Validate propTypes for lazy() and memo() and warn about invalid patterns (#14298)
29 ноя 2018, 23:06
Не верифицирован
29 ноя 2018, 23:06
d14ba87
Код
Авторство
О чём код?
/** * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ import type {LazyComponent, Thenable} from 'shared/ReactLazyComponent'; import {REACT_LAZY_TYPE} from 'shared/ReactSymbols'; import warning from 'shared/warning'; export function lazy<T, R>(ctor: () => Thenable<T, R>): LazyComponent<T> { let lazyType = { $$typeof: REACT_LAZY_TYPE, _ctor: ctor, // React uses these fields to store the result. _status: -1, _result: null, }; if (__DEV__) { // In production, this would just set it on the object. let defaultProps; let propTypes; Object.defineProperties(lazyType, { defaultProps: { configurable: true, get() { return defaultProps; }, set(newDefaultProps) { warning( false, 'React.lazy(...): It is not supported to assign `defaultProps` to ' + 'a lazy component import. Either specify them where the component ' + 'is defined, or create a wrapping component around it.', ); defaultProps = newDefaultProps; // Match production behavior more closely: Object.defineProperty(lazyType, 'defaultProps', { enumerable: true, }); }, }, propTypes: { configurable: true, get() { return propTypes; }, set(newPropTypes) { warning( false, 'React.lazy(...): It is not supported to assign `propTypes` to ' + 'a lazy component import. Either specify them where the component ' + 'is defined, or create a wrapping component around it.', ); propTypes = newPropTypes; // Match production behavior more closely: Object.defineProperty(lazyType, 'propTypes', { enumerable: true, }); }, }, }); } return lazyType; }