2017-04-20 07:45:31 +08:00
|
|
|
/**
|
2018-09-08 06:11:23 +08:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2017-04-20 07:45:31 +08:00
|
|
|
*
|
2017-09-25 04:48:13 +08:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2017-04-20 07:45:31 +08:00
|
|
|
*
|
2019-01-18 01:42:27 +08:00
|
|
|
* @flow
|
2017-04-20 07:45:31 +08:00
|
|
|
*/
|
|
|
|
|
|
2017-11-03 03:50:03 +08:00
|
|
|
import React from 'react';
|
2019-03-16 06:17:09 +08:00
|
|
|
import {isForwardRef, isMemo, ForwardRef} from 'react-is';
|
2017-11-03 03:50:03 +08:00
|
|
|
import describeComponentFrame from 'shared/describeComponentFrame';
|
|
|
|
|
import getComponentName from 'shared/getComponentName';
|
2018-06-16 01:12:45 +08:00
|
|
|
import shallowEqual from 'shared/shallowEqual';
|
2018-06-19 23:03:45 +08:00
|
|
|
import invariant from 'shared/invariant';
|
2017-11-03 03:50:03 +08:00
|
|
|
import checkPropTypes from 'prop-types/checkPropTypes';
|
2019-01-18 01:42:27 +08:00
|
|
|
import ReactSharedInternals from 'shared/ReactSharedInternals';
|
|
|
|
|
import warning from 'shared/warning';
|
|
|
|
|
import is from 'shared/objectIs';
|
|
|
|
|
|
2019-01-30 08:32:15 +08:00
|
|
|
import type {Dispatcher as DispatcherType} from 'react-reconciler/src/ReactFiberHooks';
|
2019-08-02 02:08:54 +08:00
|
|
|
import type {
|
|
|
|
|
ReactContext,
|
|
|
|
|
ReactEventResponderListener,
|
|
|
|
|
} from 'shared/ReactTypes';
|
2019-01-18 01:42:27 +08:00
|
|
|
import type {ReactElement} from 'shared/ReactElementType';
|
|
|
|
|
|
|
|
|
|
type BasicStateAction<S> = (S => S) | S;
|
|
|
|
|
type Dispatch<A> = A => void;
|
|
|
|
|
|
|
|
|
|
type Update<A> = {
|
|
|
|
|
action: A,
|
|
|
|
|
next: Update<A> | null,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type UpdateQueue<A> = {
|
2019-03-16 06:30:32 +08:00
|
|
|
first: Update<A> | null,
|
2019-01-18 01:42:27 +08:00
|
|
|
dispatch: any,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type Hook = {
|
|
|
|
|
memoizedState: any,
|
|
|
|
|
queue: UpdateQueue<any> | null,
|
|
|
|
|
next: Hook | null,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const {ReactCurrentDispatcher} = ReactSharedInternals;
|
|
|
|
|
|
|
|
|
|
const RE_RENDER_LIMIT = 25;
|
2017-04-20 07:45:31 +08:00
|
|
|
|
Inline fbjs/lib/emptyObject (#13055)
* Inline fbjs/lib/emptyObject
* Explicit naming
* Compare to undefined
* Another approach for detecting whether we can mutate
Each renderer would have its own local LegacyRefsObject function.
While in general we don't want `instanceof`, here it lets us do a simple check: did *we* create the refs object?
Then we can mutate it.
If the check didn't pass, either we're attaching ref for the first time (so we know to use the constructor),
or (unlikely) we're attaching a ref to a component owned by another renderer. In this case, to avoid "losing"
refs, we assign them onto the new object. Even in that case it shouldn't "hop" between renderers anymore.
* Clearer naming
* Add test case for strings refs across renderers
* Use a shared empty object for refs by reading it from React
* Remove string refs from ReactART test
It's not currently possible to resetModules() between several renderers
without also resetting the `React` module. However, that leads to losing
the referential identity of the empty ref object, and thus subsequent
checks in the renderers for whether it is pooled fail (and cause assignments
to a frozen object).
This has always been the case, but we used to work around it by shimming
fbjs/lib/emptyObject in tests and preserving its referential identity.
This won't work anymore because we've inlined it. And preserving referential
identity of React itself wouldn't be great because it could be confusing during
testing (although we might want to revisit this in the future by moving its
stateful parts into a separate package).
For now, I'm removing string ref usage from this test because only this is
the only place in our tests where we hit this problem, and it's only
related to string refs, and not just ref mechanism in general.
* Simplify the condition
2018-06-19 20:41:42 +08:00
|
|
|
const emptyObject = {};
|
|
|
|
|
if (__DEV__) {
|
|
|
|
|
Object.freeze(emptyObject);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-18 01:42:27 +08:00
|
|
|
// In DEV, this is the name of the currently executing primitive hook
|
|
|
|
|
let currentHookNameInDev: ?string;
|
|
|
|
|
|
|
|
|
|
function areHookInputsEqual(
|
|
|
|
|
nextDeps: Array<mixed>,
|
|
|
|
|
prevDeps: Array<mixed> | null,
|
|
|
|
|
) {
|
|
|
|
|
if (prevDeps === null) {
|
|
|
|
|
warning(
|
|
|
|
|
false,
|
|
|
|
|
'%s received a final argument during this render, but not during ' +
|
|
|
|
|
'the previous render. Even though the final argument is optional, ' +
|
|
|
|
|
'its type cannot change between renders.',
|
|
|
|
|
currentHookNameInDev,
|
|
|
|
|
);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Don't bother comparing lengths in prod because these arrays should be
|
|
|
|
|
// passed inline.
|
|
|
|
|
if (nextDeps.length !== prevDeps.length) {
|
|
|
|
|
warning(
|
|
|
|
|
false,
|
|
|
|
|
'The final argument passed to %s changed size between renders. The ' +
|
|
|
|
|
'order and size of this array must remain constant.\n\n' +
|
|
|
|
|
'Previous: %s\n' +
|
|
|
|
|
'Incoming: %s',
|
|
|
|
|
currentHookNameInDev,
|
|
|
|
|
`[${nextDeps.join(', ')}]`,
|
|
|
|
|
`[${prevDeps.join(', ')}]`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
for (let i = 0; i < prevDeps.length && i < nextDeps.length; i++) {
|
|
|
|
|
if (is(nextDeps[i], prevDeps[i])) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-10 23:15:18 +08:00
|
|
|
class Updater {
|
|
|
|
|
constructor(renderer) {
|
|
|
|
|
this._renderer = renderer;
|
|
|
|
|
this._callbacks = [];
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-18 01:42:27 +08:00
|
|
|
_renderer: ReactShallowRenderer;
|
|
|
|
|
_callbacks: Array<any>;
|
|
|
|
|
|
2018-09-10 23:15:18 +08:00
|
|
|
_enqueueCallback(callback, publicInstance) {
|
|
|
|
|
if (typeof callback === 'function' && publicInstance) {
|
|
|
|
|
this._callbacks.push({
|
|
|
|
|
callback,
|
|
|
|
|
publicInstance,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_invokeCallbacks() {
|
|
|
|
|
const callbacks = this._callbacks;
|
|
|
|
|
this._callbacks = [];
|
|
|
|
|
|
|
|
|
|
callbacks.forEach(({callback, publicInstance}) => {
|
|
|
|
|
callback.call(publicInstance);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isMounted(publicInstance) {
|
|
|
|
|
return !!this._renderer._element;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enqueueForceUpdate(publicInstance, callback, callerName) {
|
|
|
|
|
this._enqueueCallback(callback, publicInstance);
|
|
|
|
|
this._renderer._forcedUpdate = true;
|
|
|
|
|
this._renderer.render(this._renderer._element, this._renderer._context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enqueueReplaceState(publicInstance, completeState, callback, callerName) {
|
|
|
|
|
this._enqueueCallback(callback, publicInstance);
|
|
|
|
|
this._renderer._newState = completeState;
|
|
|
|
|
this._renderer.render(this._renderer._element, this._renderer._context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enqueueSetState(publicInstance, partialState, callback, callerName) {
|
|
|
|
|
this._enqueueCallback(callback, publicInstance);
|
|
|
|
|
const currentState = this._renderer._newState || publicInstance.state;
|
|
|
|
|
|
|
|
|
|
if (typeof partialState === 'function') {
|
|
|
|
|
partialState = partialState.call(
|
|
|
|
|
publicInstance,
|
|
|
|
|
currentState,
|
|
|
|
|
publicInstance.props,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Null and undefined are treated as no-ops.
|
|
|
|
|
if (partialState === null || partialState === undefined) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._renderer._newState = {
|
|
|
|
|
...currentState,
|
|
|
|
|
...partialState,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this._renderer.render(this._renderer._element, this._renderer._context);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-18 01:42:27 +08:00
|
|
|
function createHook(): Hook {
|
|
|
|
|
return {
|
|
|
|
|
memoizedState: null,
|
|
|
|
|
queue: null,
|
|
|
|
|
next: null,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function basicStateReducer<S>(state: S, action: BasicStateAction<S>): S {
|
|
|
|
|
return typeof action === 'function' ? action(state) : action;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-20 07:45:31 +08:00
|
|
|
class ReactShallowRenderer {
|
|
|
|
|
static createRenderer = function() {
|
|
|
|
|
return new ReactShallowRenderer();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
constructor() {
|
2019-03-16 06:30:32 +08:00
|
|
|
this._reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_reset() {
|
2017-04-20 07:45:31 +08:00
|
|
|
this._context = null;
|
|
|
|
|
this._element = null;
|
|
|
|
|
this._instance = null;
|
|
|
|
|
this._newState = null;
|
|
|
|
|
this._rendered = null;
|
|
|
|
|
this._rendering = false;
|
2017-10-18 21:54:06 +08:00
|
|
|
this._forcedUpdate = false;
|
2017-04-20 07:45:31 +08:00
|
|
|
this._updater = new Updater(this);
|
2019-01-24 05:28:09 +08:00
|
|
|
this._dispatcher = this._createDispatcher();
|
|
|
|
|
this._workInProgressHook = null;
|
|
|
|
|
this._firstWorkInProgressHook = null;
|
|
|
|
|
this._isReRender = false;
|
|
|
|
|
this._didScheduleRenderPhaseUpdate = false;
|
|
|
|
|
this._renderPhaseUpdates = null;
|
|
|
|
|
this._numberOfReRenders = 0;
|
2019-01-18 01:42:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_context: null | Object;
|
|
|
|
|
_newState: null | Object;
|
|
|
|
|
_instance: any;
|
|
|
|
|
_element: null | ReactElement;
|
|
|
|
|
_rendered: null | mixed;
|
|
|
|
|
_updater: Updater;
|
|
|
|
|
_rendering: boolean;
|
|
|
|
|
_forcedUpdate: boolean;
|
|
|
|
|
_dispatcher: DispatcherType;
|
|
|
|
|
_workInProgressHook: null | Hook;
|
|
|
|
|
_firstWorkInProgressHook: null | Hook;
|
|
|
|
|
_renderPhaseUpdates: Map<UpdateQueue<any>, Update<any>> | null;
|
|
|
|
|
_isReRender: boolean;
|
|
|
|
|
_didScheduleRenderPhaseUpdate: boolean;
|
|
|
|
|
_numberOfReRenders: number;
|
|
|
|
|
|
|
|
|
|
_validateCurrentlyRenderingComponent() {
|
|
|
|
|
invariant(
|
2019-03-16 06:30:32 +08:00
|
|
|
this._rendering && !this._instance,
|
2019-03-19 02:22:38 +08:00
|
|
|
'Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for' +
|
|
|
|
|
' one of the following reasons:\n' +
|
|
|
|
|
'1. You might have mismatching versions of React and the renderer (such as React DOM)\n' +
|
|
|
|
|
'2. You might be breaking the Rules of Hooks\n' +
|
|
|
|
|
'3. You might have more than one copy of React in the same app\n' +
|
|
|
|
|
'See https://fb.me/react-invalid-hook-call for tips about how to debug and fix this problem.',
|
2019-01-18 01:42:27 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_createDispatcher(): DispatcherType {
|
2019-01-30 09:39:24 +08:00
|
|
|
const useReducer = <S, I, A>(
|
2019-01-18 01:42:27 +08:00
|
|
|
reducer: (S, A) => S,
|
2019-01-30 09:39:24 +08:00
|
|
|
initialArg: I,
|
|
|
|
|
init?: I => S,
|
2019-01-18 01:42:27 +08:00
|
|
|
): [S, Dispatch<A>] => {
|
|
|
|
|
this._validateCurrentlyRenderingComponent();
|
|
|
|
|
this._createWorkInProgressHook();
|
|
|
|
|
const workInProgressHook: Hook = (this._workInProgressHook: any);
|
2019-03-16 06:30:32 +08:00
|
|
|
|
2019-01-18 01:42:27 +08:00
|
|
|
if (this._isReRender) {
|
2019-03-16 06:30:32 +08:00
|
|
|
// This is a re-render.
|
2019-01-18 01:42:27 +08:00
|
|
|
const queue: UpdateQueue<A> = (workInProgressHook.queue: any);
|
|
|
|
|
const dispatch: Dispatch<A> = (queue.dispatch: any);
|
2019-03-16 06:30:32 +08:00
|
|
|
if (this._numberOfReRenders > 0) {
|
|
|
|
|
// Apply the new render phase updates to the previous current hook.
|
|
|
|
|
if (this._renderPhaseUpdates !== null) {
|
|
|
|
|
// Render phase updates are stored in a map of queue -> linked list
|
|
|
|
|
const firstRenderPhaseUpdate = this._renderPhaseUpdates.get(queue);
|
|
|
|
|
if (firstRenderPhaseUpdate !== undefined) {
|
|
|
|
|
(this._renderPhaseUpdates: any).delete(queue);
|
|
|
|
|
let newState = workInProgressHook.memoizedState;
|
|
|
|
|
let update = firstRenderPhaseUpdate;
|
|
|
|
|
do {
|
|
|
|
|
const action = update.action;
|
|
|
|
|
newState = reducer(newState, action);
|
|
|
|
|
update = update.next;
|
|
|
|
|
} while (update !== null);
|
|
|
|
|
workInProgressHook.memoizedState = newState;
|
|
|
|
|
return [newState, dispatch];
|
|
|
|
|
}
|
2019-01-18 01:42:27 +08:00
|
|
|
}
|
2019-03-16 06:30:32 +08:00
|
|
|
return [workInProgressHook.memoizedState, dispatch];
|
2019-01-18 01:42:27 +08:00
|
|
|
}
|
2019-03-16 06:30:32 +08:00
|
|
|
// Process updates outside of render
|
|
|
|
|
let newState = workInProgressHook.memoizedState;
|
|
|
|
|
let update = queue.first;
|
|
|
|
|
if (update !== null) {
|
|
|
|
|
do {
|
|
|
|
|
const action = update.action;
|
|
|
|
|
newState = reducer(newState, action);
|
|
|
|
|
update = update.next;
|
|
|
|
|
} while (update !== null);
|
|
|
|
|
queue.first = null;
|
|
|
|
|
workInProgressHook.memoizedState = newState;
|
|
|
|
|
}
|
|
|
|
|
return [newState, dispatch];
|
2019-01-18 01:42:27 +08:00
|
|
|
} else {
|
2019-01-30 09:39:24 +08:00
|
|
|
let initialState;
|
2019-01-18 01:42:27 +08:00
|
|
|
if (reducer === basicStateReducer) {
|
|
|
|
|
// Special case for `useState`.
|
2019-01-30 09:39:24 +08:00
|
|
|
initialState =
|
|
|
|
|
typeof initialArg === 'function'
|
|
|
|
|
? ((initialArg: any): () => S)()
|
|
|
|
|
: ((initialArg: any): S);
|
|
|
|
|
} else {
|
|
|
|
|
initialState =
|
|
|
|
|
init !== undefined ? init(initialArg) : ((initialArg: any): S);
|
2019-01-18 01:42:27 +08:00
|
|
|
}
|
|
|
|
|
workInProgressHook.memoizedState = initialState;
|
|
|
|
|
const queue: UpdateQueue<A> = (workInProgressHook.queue = {
|
2019-03-16 06:30:32 +08:00
|
|
|
first: null,
|
2019-01-18 01:42:27 +08:00
|
|
|
dispatch: null,
|
|
|
|
|
});
|
|
|
|
|
const dispatch: Dispatch<
|
|
|
|
|
A,
|
2019-03-16 06:30:32 +08:00
|
|
|
> = (queue.dispatch = (this._dispatchAction.bind(this, queue): any));
|
2019-01-18 01:42:27 +08:00
|
|
|
return [workInProgressHook.memoizedState, dispatch];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const useState = <S>(
|
|
|
|
|
initialState: (() => S) | S,
|
|
|
|
|
): [S, Dispatch<BasicStateAction<S>>] => {
|
|
|
|
|
return useReducer(
|
|
|
|
|
basicStateReducer,
|
|
|
|
|
// useReducer has a special case to support lazy useState initializers
|
|
|
|
|
(initialState: any),
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const useMemo = <T>(
|
|
|
|
|
nextCreate: () => T,
|
|
|
|
|
deps: Array<mixed> | void | null,
|
|
|
|
|
): T => {
|
|
|
|
|
this._validateCurrentlyRenderingComponent();
|
|
|
|
|
this._createWorkInProgressHook();
|
|
|
|
|
|
2019-01-18 08:52:13 +08:00
|
|
|
const nextDeps = deps !== undefined ? deps : null;
|
2019-01-18 01:42:27 +08:00
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
this._workInProgressHook !== null &&
|
|
|
|
|
this._workInProgressHook.memoizedState !== null
|
|
|
|
|
) {
|
|
|
|
|
const prevState = this._workInProgressHook.memoizedState;
|
|
|
|
|
const prevDeps = prevState[1];
|
|
|
|
|
if (nextDeps !== null) {
|
|
|
|
|
if (areHookInputsEqual(nextDeps, prevDeps)) {
|
|
|
|
|
return prevState[0];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const nextValue = nextCreate();
|
|
|
|
|
(this._workInProgressHook: any).memoizedState = [nextValue, nextDeps];
|
|
|
|
|
return nextValue;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const useRef = <T>(initialValue: T): {current: T} => {
|
|
|
|
|
this._validateCurrentlyRenderingComponent();
|
|
|
|
|
this._createWorkInProgressHook();
|
|
|
|
|
const previousRef = (this._workInProgressHook: any).memoizedState;
|
|
|
|
|
if (previousRef === null) {
|
|
|
|
|
const ref = {current: initialValue};
|
|
|
|
|
if (__DEV__) {
|
|
|
|
|
Object.seal(ref);
|
|
|
|
|
}
|
|
|
|
|
(this._workInProgressHook: any).memoizedState = ref;
|
|
|
|
|
return ref;
|
|
|
|
|
} else {
|
|
|
|
|
return previousRef;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const readContext = <T>(
|
|
|
|
|
context: ReactContext<T>,
|
|
|
|
|
observedBits: void | number | boolean,
|
|
|
|
|
): T => {
|
|
|
|
|
return context._currentValue;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const noOp = () => {
|
|
|
|
|
this._validateCurrentlyRenderingComponent();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const identity = (fn: Function): Function => {
|
|
|
|
|
return fn;
|
|
|
|
|
};
|
|
|
|
|
|
2019-08-02 02:08:54 +08:00
|
|
|
const useResponder = (
|
|
|
|
|
responder,
|
|
|
|
|
props,
|
|
|
|
|
): ReactEventResponderListener<any, any> => ({
|
|
|
|
|
props: props,
|
|
|
|
|
responder,
|
|
|
|
|
});
|
|
|
|
|
|
2019-01-18 01:42:27 +08:00
|
|
|
return {
|
|
|
|
|
readContext,
|
|
|
|
|
useCallback: (identity: any),
|
|
|
|
|
useContext: <T>(context: ReactContext<T>): T => {
|
|
|
|
|
this._validateCurrentlyRenderingComponent();
|
|
|
|
|
return readContext(context);
|
|
|
|
|
},
|
|
|
|
|
useDebugValue: noOp,
|
|
|
|
|
useEffect: noOp,
|
|
|
|
|
useImperativeHandle: noOp,
|
|
|
|
|
useLayoutEffect: noOp,
|
|
|
|
|
useMemo,
|
|
|
|
|
useReducer,
|
|
|
|
|
useRef,
|
|
|
|
|
useState,
|
2019-08-02 02:08:54 +08:00
|
|
|
useResponder,
|
2019-01-18 01:42:27 +08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-16 06:30:32 +08:00
|
|
|
_dispatchAction<A>(queue: UpdateQueue<A>, action: A) {
|
2019-01-18 01:42:27 +08:00
|
|
|
invariant(
|
|
|
|
|
this._numberOfReRenders < RE_RENDER_LIMIT,
|
|
|
|
|
'Too many re-renders. React limits the number of renders to prevent ' +
|
|
|
|
|
'an infinite loop.',
|
|
|
|
|
);
|
|
|
|
|
|
2019-03-16 06:30:32 +08:00
|
|
|
if (this._rendering) {
|
2019-01-18 01:42:27 +08:00
|
|
|
// This is a render phase update. Stash it in a lazily-created map of
|
|
|
|
|
// queue -> linked list of updates. After this render pass, we'll restart
|
|
|
|
|
// and apply the stashed updates on top of the work-in-progress hook.
|
|
|
|
|
this._didScheduleRenderPhaseUpdate = true;
|
|
|
|
|
const update: Update<A> = {
|
|
|
|
|
action,
|
|
|
|
|
next: null,
|
|
|
|
|
};
|
|
|
|
|
let renderPhaseUpdates = this._renderPhaseUpdates;
|
|
|
|
|
if (renderPhaseUpdates === null) {
|
|
|
|
|
this._renderPhaseUpdates = renderPhaseUpdates = new Map();
|
|
|
|
|
}
|
|
|
|
|
const firstRenderPhaseUpdate = renderPhaseUpdates.get(queue);
|
|
|
|
|
if (firstRenderPhaseUpdate === undefined) {
|
|
|
|
|
renderPhaseUpdates.set(queue, update);
|
|
|
|
|
} else {
|
|
|
|
|
// Append the update to the end of the list.
|
|
|
|
|
let lastRenderPhaseUpdate = firstRenderPhaseUpdate;
|
|
|
|
|
while (lastRenderPhaseUpdate.next !== null) {
|
|
|
|
|
lastRenderPhaseUpdate = lastRenderPhaseUpdate.next;
|
|
|
|
|
}
|
|
|
|
|
lastRenderPhaseUpdate.next = update;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-03-16 06:30:32 +08:00
|
|
|
const update: Update<A> = {
|
|
|
|
|
action,
|
|
|
|
|
next: null,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Append the update to the end of the list.
|
|
|
|
|
let last = queue.first;
|
|
|
|
|
if (last === null) {
|
|
|
|
|
queue.first = update;
|
|
|
|
|
} else {
|
|
|
|
|
while (last.next !== null) {
|
|
|
|
|
last = last.next;
|
|
|
|
|
}
|
|
|
|
|
last.next = update;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Re-render now.
|
|
|
|
|
this.render(this._element, this._context);
|
2019-01-18 01:42:27 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_createWorkInProgressHook(): Hook {
|
|
|
|
|
if (this._workInProgressHook === null) {
|
|
|
|
|
// This is the first hook in the list
|
|
|
|
|
if (this._firstWorkInProgressHook === null) {
|
|
|
|
|
this._isReRender = false;
|
|
|
|
|
this._firstWorkInProgressHook = this._workInProgressHook = createHook();
|
|
|
|
|
} else {
|
|
|
|
|
// There's already a work-in-progress. Reuse it.
|
|
|
|
|
this._isReRender = true;
|
|
|
|
|
this._workInProgressHook = this._firstWorkInProgressHook;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (this._workInProgressHook.next === null) {
|
|
|
|
|
this._isReRender = false;
|
|
|
|
|
// Append to the end of the list
|
|
|
|
|
this._workInProgressHook = (this
|
|
|
|
|
._workInProgressHook: any).next = createHook();
|
|
|
|
|
} else {
|
|
|
|
|
// There's already a work-in-progress. Reuse it.
|
|
|
|
|
this._isReRender = true;
|
|
|
|
|
this._workInProgressHook = this._workInProgressHook.next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return this._workInProgressHook;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_finishHooks(element: ReactElement, context: null | Object) {
|
|
|
|
|
if (this._didScheduleRenderPhaseUpdate) {
|
|
|
|
|
// Updates were scheduled during the render phase. They are stored in
|
|
|
|
|
// the `renderPhaseUpdates` map. Call the component again, reusing the
|
|
|
|
|
// work-in-progress hooks and applying the additional updates on top. Keep
|
|
|
|
|
// restarting until no more updates are scheduled.
|
|
|
|
|
this._didScheduleRenderPhaseUpdate = false;
|
|
|
|
|
this._numberOfReRenders += 1;
|
|
|
|
|
|
|
|
|
|
// Start over from the beginning of the list
|
|
|
|
|
this._workInProgressHook = null;
|
|
|
|
|
this._rendering = false;
|
|
|
|
|
this.render(element, context);
|
|
|
|
|
} else {
|
|
|
|
|
this._workInProgressHook = null;
|
|
|
|
|
this._renderPhaseUpdates = null;
|
|
|
|
|
this._numberOfReRenders = 0;
|
|
|
|
|
}
|
2017-04-20 07:45:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getMountedInstance() {
|
|
|
|
|
return this._instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getRenderOutput() {
|
|
|
|
|
return this._rendered;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-18 01:42:27 +08:00
|
|
|
render(element: ReactElement | null, context: null | Object = emptyObject) {
|
2017-04-20 07:45:31 +08:00
|
|
|
invariant(
|
|
|
|
|
React.isValidElement(element),
|
|
|
|
|
'ReactShallowRenderer render(): Invalid component element.%s',
|
|
|
|
|
typeof element === 'function'
|
|
|
|
|
? ' Instead of passing a component class, make sure to instantiate ' +
|
2017-11-08 02:09:33 +08:00
|
|
|
'it by passing it to React.createElement.'
|
2017-04-20 07:45:31 +08:00
|
|
|
: '',
|
|
|
|
|
);
|
2019-01-18 01:42:27 +08:00
|
|
|
element = ((element: any): ReactElement);
|
2017-11-04 00:16:56 +08:00
|
|
|
// Show a special message for host elements since it's a common case.
|
2017-04-20 07:45:31 +08:00
|
|
|
invariant(
|
|
|
|
|
typeof element.type !== 'string',
|
|
|
|
|
'ReactShallowRenderer render(): Shallow rendering works only with custom ' +
|
|
|
|
|
'components, not primitives (%s). Instead of calling `.render(el)` and ' +
|
|
|
|
|
'inspecting the rendered output, look at `el.props` directly instead.',
|
|
|
|
|
element.type,
|
|
|
|
|
);
|
2017-11-04 00:16:56 +08:00
|
|
|
invariant(
|
2019-03-16 06:17:09 +08:00
|
|
|
isForwardRef(element) ||
|
|
|
|
|
(typeof element.type === 'function' || isMemo(element.type)),
|
2017-11-04 00:16:56 +08:00
|
|
|
'ReactShallowRenderer render(): Shallow rendering works only with custom ' +
|
|
|
|
|
'components, but the provided element type was `%s`.',
|
|
|
|
|
Array.isArray(element.type)
|
|
|
|
|
? 'array'
|
2018-07-18 03:18:34 +08:00
|
|
|
: element.type === null
|
|
|
|
|
? 'null'
|
|
|
|
|
: typeof element.type,
|
2017-11-04 00:16:56 +08:00
|
|
|
);
|
2017-04-20 07:45:31 +08:00
|
|
|
|
|
|
|
|
if (this._rendering) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-03-16 06:30:32 +08:00
|
|
|
if (this._element != null && this._element.type !== element.type) {
|
|
|
|
|
this._reset();
|
|
|
|
|
}
|
2017-04-20 07:45:31 +08:00
|
|
|
|
2019-03-16 06:17:09 +08:00
|
|
|
const elementType = isMemo(element.type) ? element.type.type : element.type;
|
|
|
|
|
const previousElement = this._element;
|
|
|
|
|
|
2017-04-20 07:45:31 +08:00
|
|
|
this._rendering = true;
|
|
|
|
|
this._element = element;
|
2019-03-16 06:17:09 +08:00
|
|
|
this._context = getMaskedContext(elementType.contextTypes, context);
|
|
|
|
|
|
|
|
|
|
// Inner memo component props aren't currently validated in createElement.
|
|
|
|
|
if (isMemo(element.type) && elementType.propTypes) {
|
|
|
|
|
currentlyValidatingElement = element;
|
|
|
|
|
checkPropTypes(
|
|
|
|
|
elementType.propTypes,
|
|
|
|
|
element.props,
|
|
|
|
|
'prop',
|
|
|
|
|
getComponentName(elementType),
|
|
|
|
|
getStackAddendum,
|
|
|
|
|
);
|
|
|
|
|
}
|
2017-04-20 07:45:31 +08:00
|
|
|
|
|
|
|
|
if (this._instance) {
|
2019-03-16 06:17:09 +08:00
|
|
|
this._updateClassComponent(elementType, element, this._context);
|
2017-04-20 07:45:31 +08:00
|
|
|
} else {
|
2019-03-16 06:17:09 +08:00
|
|
|
if (shouldConstruct(elementType)) {
|
|
|
|
|
this._instance = new elementType(
|
2017-04-20 07:45:31 +08:00
|
|
|
element.props,
|
2018-01-06 02:49:57 +08:00
|
|
|
this._context,
|
2017-04-20 07:45:31 +08:00
|
|
|
this._updater,
|
|
|
|
|
);
|
2019-03-16 06:17:09 +08:00
|
|
|
if (typeof elementType.getDerivedStateFromProps === 'function') {
|
|
|
|
|
const partialState = elementType.getDerivedStateFromProps.call(
|
2019-01-18 10:31:14 +08:00
|
|
|
null,
|
|
|
|
|
element.props,
|
|
|
|
|
this._instance.state,
|
|
|
|
|
);
|
|
|
|
|
if (partialState != null) {
|
|
|
|
|
this._instance.state = Object.assign(
|
|
|
|
|
{},
|
|
|
|
|
this._instance.state,
|
|
|
|
|
partialState,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-20 01:36:46 +08:00
|
|
|
|
2019-03-16 06:17:09 +08:00
|
|
|
if (elementType.contextTypes) {
|
2017-07-15 06:36:24 +08:00
|
|
|
currentlyValidatingElement = element;
|
2017-04-20 07:45:31 +08:00
|
|
|
checkPropTypes(
|
2019-03-16 06:17:09 +08:00
|
|
|
elementType.contextTypes,
|
2018-01-06 02:49:57 +08:00
|
|
|
this._context,
|
2017-04-20 07:45:31 +08:00
|
|
|
'context',
|
2019-03-16 06:17:09 +08:00
|
|
|
getName(elementType, this._instance),
|
2017-07-15 06:36:24 +08:00
|
|
|
getStackAddendum,
|
2017-04-20 07:45:31 +08:00
|
|
|
);
|
|
|
|
|
|
2017-07-15 06:36:24 +08:00
|
|
|
currentlyValidatingElement = null;
|
2017-04-20 07:45:31 +08:00
|
|
|
}
|
|
|
|
|
|
2019-03-16 06:17:09 +08:00
|
|
|
this._mountClassComponent(elementType, element, this._context);
|
2017-04-20 07:45:31 +08:00
|
|
|
} else {
|
2019-03-16 06:17:09 +08:00
|
|
|
let shouldRender = true;
|
2019-03-16 06:30:32 +08:00
|
|
|
if (isMemo(element.type) && previousElement !== null) {
|
2019-03-16 06:17:09 +08:00
|
|
|
// This is a Memo component that is being re-rendered.
|
|
|
|
|
const compare = element.type.compare || shallowEqual;
|
|
|
|
|
if (compare(previousElement.props, element.props)) {
|
|
|
|
|
shouldRender = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (shouldRender) {
|
|
|
|
|
const prevDispatcher = ReactCurrentDispatcher.current;
|
|
|
|
|
ReactCurrentDispatcher.current = this._dispatcher;
|
|
|
|
|
try {
|
|
|
|
|
// elementType could still be a ForwardRef if it was
|
|
|
|
|
// nested inside Memo.
|
|
|
|
|
if (elementType.$$typeof === ForwardRef) {
|
|
|
|
|
invariant(
|
|
|
|
|
typeof elementType.render === 'function',
|
|
|
|
|
'forwardRef requires a render function but was given %s.',
|
|
|
|
|
typeof elementType.render,
|
|
|
|
|
);
|
|
|
|
|
this._rendered = elementType.render.call(
|
|
|
|
|
undefined,
|
|
|
|
|
element.props,
|
|
|
|
|
element.ref,
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
this._rendered = elementType(element.props, this._context);
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
ReactCurrentDispatcher.current = prevDispatcher;
|
2019-03-15 23:28:34 +08:00
|
|
|
}
|
2019-03-16 06:17:09 +08:00
|
|
|
this._finishHooks(element, context);
|
2019-01-18 01:42:27 +08:00
|
|
|
}
|
2017-04-20 07:45:31 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._rendering = false;
|
2017-11-23 06:15:11 +08:00
|
|
|
this._updater._invokeCallbacks();
|
2017-04-20 07:45:31 +08:00
|
|
|
|
|
|
|
|
return this.getRenderOutput();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unmount() {
|
|
|
|
|
if (this._instance) {
|
|
|
|
|
if (typeof this._instance.componentWillUnmount === 'function') {
|
|
|
|
|
this._instance.componentWillUnmount();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-16 06:30:32 +08:00
|
|
|
this._reset();
|
2017-04-20 07:45:31 +08:00
|
|
|
}
|
|
|
|
|
|
2019-03-16 06:17:09 +08:00
|
|
|
_mountClassComponent(
|
|
|
|
|
elementType: Function,
|
|
|
|
|
element: ReactElement,
|
|
|
|
|
context: null | Object,
|
|
|
|
|
) {
|
2017-04-20 07:45:31 +08:00
|
|
|
this._instance.context = context;
|
2018-01-20 01:36:46 +08:00
|
|
|
this._instance.props = element.props;
|
2018-01-06 02:44:44 +08:00
|
|
|
this._instance.state = this._instance.state || null;
|
2017-04-20 07:45:31 +08:00
|
|
|
this._instance.updater = this._updater;
|
|
|
|
|
|
2018-01-20 01:36:46 +08:00
|
|
|
if (
|
|
|
|
|
typeof this._instance.UNSAFE_componentWillMount === 'function' ||
|
|
|
|
|
typeof this._instance.componentWillMount === 'function'
|
|
|
|
|
) {
|
2017-04-20 07:45:31 +08:00
|
|
|
const beforeState = this._newState;
|
|
|
|
|
|
2018-03-27 04:28:10 +08:00
|
|
|
// In order to support react-lifecycles-compat polyfilled components,
|
|
|
|
|
// Unsafe lifecycles should not be invoked for components using the new APIs.
|
2018-02-02 03:15:57 +08:00
|
|
|
if (
|
2019-03-16 06:17:09 +08:00
|
|
|
typeof elementType.getDerivedStateFromProps !== 'function' &&
|
2018-03-27 04:28:10 +08:00
|
|
|
typeof this._instance.getSnapshotBeforeUpdate !== 'function'
|
2018-02-02 03:15:57 +08:00
|
|
|
) {
|
2018-03-27 04:28:10 +08:00
|
|
|
if (typeof this._instance.componentWillMount === 'function') {
|
|
|
|
|
this._instance.componentWillMount();
|
|
|
|
|
}
|
|
|
|
|
if (typeof this._instance.UNSAFE_componentWillMount === 'function') {
|
|
|
|
|
this._instance.UNSAFE_componentWillMount();
|
|
|
|
|
}
|
2018-01-20 01:36:46 +08:00
|
|
|
}
|
2017-04-20 07:45:31 +08:00
|
|
|
|
|
|
|
|
// setState may have been called during cWM
|
|
|
|
|
if (beforeState !== this._newState) {
|
|
|
|
|
this._instance.state = this._newState || emptyObject;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._rendered = this._instance.render();
|
2017-08-04 05:49:29 +08:00
|
|
|
// Intentionally do not call componentDidMount()
|
|
|
|
|
// because DOM refs are not available.
|
2017-04-20 07:45:31 +08:00
|
|
|
}
|
|
|
|
|
|
2019-03-16 06:17:09 +08:00
|
|
|
_updateClassComponent(
|
|
|
|
|
elementType: Function,
|
|
|
|
|
element: ReactElement,
|
|
|
|
|
context: null | Object,
|
|
|
|
|
) {
|
|
|
|
|
const {props} = element;
|
2018-01-20 01:36:46 +08:00
|
|
|
|
2017-11-03 22:29:14 +08:00
|
|
|
const oldState = this._instance.state || emptyObject;
|
2017-04-20 07:45:31 +08:00
|
|
|
const oldProps = this._instance.props;
|
|
|
|
|
|
2018-01-20 01:36:46 +08:00
|
|
|
if (oldProps !== props) {
|
2018-03-27 04:28:10 +08:00
|
|
|
// In order to support react-lifecycles-compat polyfilled components,
|
|
|
|
|
// Unsafe lifecycles should not be invoked for components using the new APIs.
|
2018-02-02 03:15:57 +08:00
|
|
|
if (
|
2019-03-16 06:17:09 +08:00
|
|
|
typeof elementType.getDerivedStateFromProps !== 'function' &&
|
2018-03-27 04:28:10 +08:00
|
|
|
typeof this._instance.getSnapshotBeforeUpdate !== 'function'
|
2018-01-20 01:36:46 +08:00
|
|
|
) {
|
2018-03-27 04:28:10 +08:00
|
|
|
if (typeof this._instance.componentWillReceiveProps === 'function') {
|
|
|
|
|
this._instance.componentWillReceiveProps(props, context);
|
|
|
|
|
}
|
|
|
|
|
if (
|
|
|
|
|
typeof this._instance.UNSAFE_componentWillReceiveProps === 'function'
|
|
|
|
|
) {
|
|
|
|
|
this._instance.UNSAFE_componentWillReceiveProps(props, context);
|
|
|
|
|
}
|
2018-01-20 01:36:46 +08:00
|
|
|
}
|
2017-04-20 07:45:31 +08:00
|
|
|
}
|
2018-01-20 01:36:46 +08:00
|
|
|
|
2017-04-20 07:45:31 +08:00
|
|
|
// Read state after cWRP in case it calls setState
|
2019-01-18 10:31:14 +08:00
|
|
|
let state = this._newState || oldState;
|
2019-03-16 06:17:09 +08:00
|
|
|
if (typeof elementType.getDerivedStateFromProps === 'function') {
|
|
|
|
|
const partialState = elementType.getDerivedStateFromProps.call(
|
2019-01-18 10:31:14 +08:00
|
|
|
null,
|
|
|
|
|
props,
|
|
|
|
|
state,
|
|
|
|
|
);
|
|
|
|
|
if (partialState != null) {
|
|
|
|
|
state = Object.assign({}, state, partialState);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-20 07:45:31 +08:00
|
|
|
|
2017-11-03 22:29:14 +08:00
|
|
|
let shouldUpdate = true;
|
|
|
|
|
if (this._forcedUpdate) {
|
|
|
|
|
shouldUpdate = true;
|
|
|
|
|
this._forcedUpdate = false;
|
|
|
|
|
} else if (typeof this._instance.shouldComponentUpdate === 'function') {
|
|
|
|
|
shouldUpdate = !!this._instance.shouldComponentUpdate(
|
|
|
|
|
props,
|
|
|
|
|
state,
|
|
|
|
|
context,
|
|
|
|
|
);
|
2019-03-16 06:17:09 +08:00
|
|
|
} else if (
|
|
|
|
|
elementType.prototype &&
|
|
|
|
|
elementType.prototype.isPureReactComponent
|
|
|
|
|
) {
|
2017-11-03 22:29:14 +08:00
|
|
|
shouldUpdate =
|
|
|
|
|
!shallowEqual(oldProps, props) || !shallowEqual(oldState, state);
|
2017-04-20 07:45:31 +08:00
|
|
|
}
|
|
|
|
|
|
2017-11-03 22:29:14 +08:00
|
|
|
if (shouldUpdate) {
|
2018-03-27 04:28:10 +08:00
|
|
|
// In order to support react-lifecycles-compat polyfilled components,
|
|
|
|
|
// Unsafe lifecycles should not be invoked for components using the new APIs.
|
2018-02-02 03:15:57 +08:00
|
|
|
if (
|
2019-03-16 06:17:09 +08:00
|
|
|
typeof elementType.getDerivedStateFromProps !== 'function' &&
|
2018-03-27 04:28:10 +08:00
|
|
|
typeof this._instance.getSnapshotBeforeUpdate !== 'function'
|
2018-01-20 01:36:46 +08:00
|
|
|
) {
|
2018-03-27 04:28:10 +08:00
|
|
|
if (typeof this._instance.componentWillUpdate === 'function') {
|
|
|
|
|
this._instance.componentWillUpdate(props, state, context);
|
|
|
|
|
}
|
|
|
|
|
if (typeof this._instance.UNSAFE_componentWillUpdate === 'function') {
|
|
|
|
|
this._instance.UNSAFE_componentWillUpdate(props, state, context);
|
|
|
|
|
}
|
2017-11-03 22:29:14 +08:00
|
|
|
}
|
2017-04-20 07:45:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._instance.context = context;
|
|
|
|
|
this._instance.props = props;
|
|
|
|
|
this._instance.state = state;
|
2019-01-18 10:31:14 +08:00
|
|
|
this._newState = null;
|
2017-04-20 07:45:31 +08:00
|
|
|
|
2017-11-03 22:29:14 +08:00
|
|
|
if (shouldUpdate) {
|
|
|
|
|
this._rendered = this._instance.render();
|
|
|
|
|
}
|
2017-08-04 05:49:29 +08:00
|
|
|
// Intentionally do not call componentDidUpdate()
|
|
|
|
|
// because DOM refs are not available.
|
2017-04-20 07:45:31 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-01 08:01:45 +08:00
|
|
|
let currentlyValidatingElement = null;
|
2017-08-03 00:57:41 +08:00
|
|
|
|
|
|
|
|
function getDisplayName(element) {
|
|
|
|
|
if (element == null) {
|
|
|
|
|
return '#empty';
|
|
|
|
|
} else if (typeof element === 'string' || typeof element === 'number') {
|
|
|
|
|
return '#text';
|
|
|
|
|
} else if (typeof element.type === 'string') {
|
|
|
|
|
return element.type;
|
|
|
|
|
} else {
|
2019-03-16 06:17:09 +08:00
|
|
|
const elementType = isMemo(element.type) ? element.type.type : element.type;
|
|
|
|
|
return elementType.displayName || elementType.name || 'Unknown';
|
2017-08-03 00:57:41 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getStackAddendum() {
|
2017-12-01 08:01:45 +08:00
|
|
|
let stack = '';
|
2017-08-03 00:57:41 +08:00
|
|
|
if (currentlyValidatingElement) {
|
2017-12-01 08:01:45 +08:00
|
|
|
const name = getDisplayName(currentlyValidatingElement);
|
|
|
|
|
const owner = currentlyValidatingElement._owner;
|
2017-08-03 00:57:41 +08:00
|
|
|
stack += describeComponentFrame(
|
|
|
|
|
name,
|
|
|
|
|
currentlyValidatingElement._source,
|
2018-07-12 22:32:06 +08:00
|
|
|
owner && getComponentName(owner.type),
|
2017-08-03 00:57:41 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return stack;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-20 07:45:31 +08:00
|
|
|
function getName(type, instance) {
|
2017-12-01 08:01:45 +08:00
|
|
|
const constructor = instance && instance.constructor;
|
2017-04-21 02:18:33 +08:00
|
|
|
return (
|
|
|
|
|
type.displayName ||
|
2017-04-20 07:45:31 +08:00
|
|
|
(constructor && constructor.displayName) ||
|
|
|
|
|
type.name ||
|
|
|
|
|
(constructor && constructor.name) ||
|
2017-04-21 02:18:33 +08:00
|
|
|
null
|
|
|
|
|
);
|
2017-04-20 07:45:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function shouldConstruct(Component) {
|
|
|
|
|
return !!(Component.prototype && Component.prototype.isReactComponent);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-06 02:49:57 +08:00
|
|
|
function getMaskedContext(contextTypes, unmaskedContext) {
|
2019-01-18 01:42:27 +08:00
|
|
|
if (!contextTypes || !unmaskedContext) {
|
2018-01-06 02:49:57 +08:00
|
|
|
return emptyObject;
|
|
|
|
|
}
|
|
|
|
|
const context = {};
|
|
|
|
|
for (let key in contextTypes) {
|
|
|
|
|
context[key] = unmaskedContext[key];
|
|
|
|
|
}
|
|
|
|
|
return context;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-03 03:50:03 +08:00
|
|
|
export default ReactShallowRenderer;
|