mirror of
https://github.com/facebook/react.git
synced 2026-11-18 20:15:56 +00:00
Remove unstable_asyncRender() API in favor of always using a wrapper
This commit is contained in:
1 parent
abd07dffb3
commit
f69748601d
6 files changed
+152
-224
No files matched your search
@@ -387,7 +387,6 @@ function renderSubtreeIntoContainer(
|
||||
parentComponent: ?ReactComponent<any, any, any>,
|
||||
children: ReactNodeList,
|
||||
containerNode: DOMContainerElement | Document,
|
||||
async: boolean,
|
||||
callback: ?Function,
|
||||
) {
|
||||
validateContainer(containerNode);
|
||||
@@ -401,104 +400,84 @@ function renderSubtreeIntoContainer(
|
||||
while (container.lastChild) {
|
||||
container.removeChild(container.lastChild);
|
||||
}
|
||||
const newRoot = async
|
||||
? DOMRenderer.createAsyncContainer(container)
|
||||
: DOMRenderer.createContainer(container);
|
||||
const newRoot = DOMRenderer.createContainer(container);
|
||||
root = (container._reactRootContainer = newRoot);
|
||||
// Initial mount should not be batched.
|
||||
DOMRenderer.unbatchedUpdates(() => {
|
||||
DOMRenderer.updateContainer(children, newRoot, parentComponent, callback);
|
||||
});
|
||||
} else {
|
||||
if (async) {
|
||||
DOMRenderer.updateAsyncContainer(
|
||||
children,
|
||||
root,
|
||||
parentComponent,
|
||||
callback,
|
||||
);
|
||||
} else {
|
||||
DOMRenderer.updateContainer(children, root, parentComponent, callback);
|
||||
}
|
||||
DOMRenderer.updateContainer(children, root, parentComponent, callback);
|
||||
}
|
||||
return DOMRenderer.getPublicRootInstance(root);
|
||||
}
|
||||
|
||||
function render(
|
||||
element: ReactElement<any>,
|
||||
container: DOMContainerElement,
|
||||
async: boolean,
|
||||
callback: ?Function,
|
||||
) {
|
||||
validateContainer(container);
|
||||
|
||||
if (ReactFeatureFlags.disableNewFiberFeatures) {
|
||||
// Top-level check occurs here instead of inside child reconciler because
|
||||
// because requirements vary between renderers. E.g. React Art
|
||||
// allows arrays.
|
||||
if (!isValidElement(element)) {
|
||||
if (typeof element === 'string') {
|
||||
invariant(
|
||||
false,
|
||||
'ReactDOM.render(): Invalid component element. Instead of ' +
|
||||
"passing a string like 'div', pass " +
|
||||
"React.createElement('div') or <div />.",
|
||||
);
|
||||
} else if (typeof element === 'function') {
|
||||
invariant(
|
||||
false,
|
||||
'ReactDOM.render(): Invalid component element. Instead of ' +
|
||||
'passing a class like Foo, pass React.createElement(Foo) ' +
|
||||
'or <Foo />.',
|
||||
);
|
||||
} else if (element != null && typeof element.props !== 'undefined') {
|
||||
// Check if it quacks like an element
|
||||
invariant(
|
||||
false,
|
||||
'ReactDOM.render(): Invalid component element. This may be ' +
|
||||
'caused by unintentionally loading two independent copies ' +
|
||||
'of React.',
|
||||
);
|
||||
} else {
|
||||
invariant(false, 'ReactDOM.render(): Invalid component element.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (__DEV__) {
|
||||
const isRootRenderedBySomeReact = !!container._reactRootContainer;
|
||||
const rootEl = getReactRootElementInContainer(container);
|
||||
const hasNonRootReactChild = !!(rootEl &&
|
||||
ReactDOMComponentTree.getInstanceFromNode(rootEl));
|
||||
|
||||
warning(
|
||||
!hasNonRootReactChild || isRootRenderedBySomeReact,
|
||||
'render(...): Replacing React-rendered children with a new root ' +
|
||||
'component. If you intended to update the children of this node, ' +
|
||||
'you should instead have the existing children update their state ' +
|
||||
'and render the new components instead of calling ReactDOM.render.',
|
||||
);
|
||||
|
||||
warning(
|
||||
!container.tagName || container.tagName.toUpperCase() !== 'BODY',
|
||||
'render(): Rendering components directly into document.body is ' +
|
||||
'discouraged, since its children are often manipulated by third-party ' +
|
||||
'scripts and browser extensions. This may lead to subtle ' +
|
||||
'reconciliation issues. Try rendering into a container element created ' +
|
||||
'for your app.',
|
||||
);
|
||||
}
|
||||
|
||||
return renderSubtreeIntoContainer(null, element, container, async, callback);
|
||||
}
|
||||
|
||||
var ReactDOM = {
|
||||
render(
|
||||
element: ReactElement<any>,
|
||||
container: DOMContainerElement,
|
||||
callback: ?Function,
|
||||
) {
|
||||
return render(element, container, false, callback);
|
||||
validateContainer(container);
|
||||
|
||||
if (ReactFeatureFlags.disableNewFiberFeatures) {
|
||||
// Top-level check occurs here instead of inside child reconciler because
|
||||
// because requirements vary between renderers. E.g. React Art
|
||||
// allows arrays.
|
||||
if (!isValidElement(element)) {
|
||||
if (typeof element === 'string') {
|
||||
invariant(
|
||||
false,
|
||||
'ReactDOM.render(): Invalid component element. Instead of ' +
|
||||
"passing a string like 'div', pass " +
|
||||
"React.createElement('div') or <div />.",
|
||||
);
|
||||
} else if (typeof element === 'function') {
|
||||
invariant(
|
||||
false,
|
||||
'ReactDOM.render(): Invalid component element. Instead of ' +
|
||||
'passing a class like Foo, pass React.createElement(Foo) ' +
|
||||
'or <Foo />.',
|
||||
);
|
||||
} else if (element != null && typeof element.props !== 'undefined') {
|
||||
// Check if it quacks like an element
|
||||
invariant(
|
||||
false,
|
||||
'ReactDOM.render(): Invalid component element. This may be ' +
|
||||
'caused by unintentionally loading two independent copies ' +
|
||||
'of React.',
|
||||
);
|
||||
} else {
|
||||
invariant(false, 'ReactDOM.render(): Invalid component element.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (__DEV__) {
|
||||
const isRootRenderedBySomeReact = !!container._reactRootContainer;
|
||||
const rootEl = getReactRootElementInContainer(container);
|
||||
const hasNonRootReactChild = !!(rootEl &&
|
||||
ReactDOMComponentTree.getInstanceFromNode(rootEl));
|
||||
|
||||
warning(
|
||||
!hasNonRootReactChild || isRootRenderedBySomeReact,
|
||||
'render(...): Replacing React-rendered children with a new root ' +
|
||||
'component. If you intended to update the children of this node, ' +
|
||||
'you should instead have the existing children update their state ' +
|
||||
'and render the new components instead of calling ReactDOM.render.',
|
||||
);
|
||||
|
||||
warning(
|
||||
!container.tagName || container.tagName.toUpperCase() !== 'BODY',
|
||||
'render(): Rendering components directly into document.body is ' +
|
||||
'discouraged, since its children are often manipulated by third-party ' +
|
||||
'scripts and browser extensions. This may lead to subtle ' +
|
||||
'reconciliation issues. Try rendering into a container element created ' +
|
||||
'for your app.',
|
||||
);
|
||||
}
|
||||
|
||||
return renderSubtreeIntoContainer(null, element, container, callback);
|
||||
},
|
||||
|
||||
unstable_renderSubtreeIntoContainer(
|
||||
@@ -515,17 +494,10 @@ var ReactDOM = {
|
||||
parentComponent,
|
||||
element,
|
||||
containerNode,
|
||||
false,
|
||||
callback,
|
||||
);
|
||||
},
|
||||
|
||||
unstable_asyncRender: (null: ?(
|
||||
element: ReactElement<any>,
|
||||
container: DOMContainerElement,
|
||||
callback: ?Function,
|
||||
) => *),
|
||||
|
||||
unmountComponentAtNode(container: DOMContainerElement) {
|
||||
invariant(
|
||||
isValidContainer(container),
|
||||
@@ -547,7 +519,7 @@ var ReactDOM = {
|
||||
|
||||
// Unmount should not be batched.
|
||||
return DOMRenderer.unbatchedUpdates(() => {
|
||||
return renderSubtreeIntoContainer(null, null, container, false, () => {
|
||||
return renderSubtreeIntoContainer(null, null, container, () => {
|
||||
container._reactRootContainer = null;
|
||||
});
|
||||
});
|
||||
@@ -581,21 +553,6 @@ var ReactDOM = {
|
||||
},
|
||||
};
|
||||
|
||||
if (ReactDOMFeatureFlags.enableAsyncSubtreeAPI) {
|
||||
ReactDOM.unstable_asyncRender = function(
|
||||
element: ReactElement<any>,
|
||||
container: DOMContainerElement,
|
||||
callback: ?Function,
|
||||
) {
|
||||
return render(element, container, true, callback);
|
||||
};
|
||||
} else {
|
||||
// We set this to null on the ReactDOM export, then delete if the feature
|
||||
// flag is not enabled so that it's undiscoverable.
|
||||
// TODO: Is there a better way to satisfy Flow?
|
||||
delete ReactDOM.unstable_asyncRender;
|
||||
}
|
||||
|
||||
if (typeof injectInternals === 'function') {
|
||||
injectInternals({
|
||||
findFiberByHostInstance: ReactDOMComponentTree.getClosestInstanceFromNode,
|
||||
|
||||
@@ -38,44 +38,11 @@ describe('ReactDOMFiberAsync', () => {
|
||||
ReactDOM = require('react-dom');
|
||||
});
|
||||
|
||||
it('unstable_asyncRender creates an async tree', () => {
|
||||
ReactDOM.unstable_asyncRender(<div>Hi</div>, container);
|
||||
expect(container.textContent).toEqual('');
|
||||
jest.runAllTimers();
|
||||
expect(container.textContent).toEqual('Hi');
|
||||
|
||||
ReactDOM.unstable_asyncRender(<div>Bye</div>, container);
|
||||
expect(container.textContent).toEqual('Hi');
|
||||
jest.runAllTimers();
|
||||
expect(container.textContent).toEqual('Bye');
|
||||
});
|
||||
|
||||
it('updates inside an async tree are async by default', () => {
|
||||
let instance;
|
||||
class Component extends React.Component {
|
||||
state = {step: 0};
|
||||
render() {
|
||||
instance = this;
|
||||
return <div>{this.state.step}</div>;
|
||||
}
|
||||
}
|
||||
|
||||
ReactDOM.unstable_asyncRender(<Component />, container);
|
||||
expect(container.textContent).toEqual('');
|
||||
jest.runAllTimers();
|
||||
expect(container.textContent).toEqual('0');
|
||||
|
||||
instance.setState({step: 1});
|
||||
expect(container.textContent).toEqual('0');
|
||||
jest.runAllTimers();
|
||||
expect(container.textContent).toEqual('1');
|
||||
});
|
||||
|
||||
it('unstable_asyncUpdates creates an async subtree', () => {
|
||||
let instance;
|
||||
class Component extends React.Component {
|
||||
state = {step: 0};
|
||||
unstable_asyncUpdates = true;
|
||||
static unstable_asyncUpdates = true;
|
||||
render() {
|
||||
instance = this;
|
||||
return <div>{this.state.step}</div>;
|
||||
@@ -93,7 +60,7 @@ describe('ReactDOMFiberAsync', () => {
|
||||
|
||||
it('updates inside an async subtree are async by default', () => {
|
||||
class Component extends React.Component {
|
||||
unstable_asyncUpdates = true;
|
||||
static unstable_asyncUpdates = true;
|
||||
render() {
|
||||
return <Child />;
|
||||
}
|
||||
@@ -116,6 +83,51 @@ describe('ReactDOMFiberAsync', () => {
|
||||
jest.runAllTimers();
|
||||
expect(container.textContent).toEqual('1');
|
||||
});
|
||||
|
||||
it('unstable_asyncUpdates at the root makes the entire tree async', () => {
|
||||
class Async extends React.Component {
|
||||
static unstable_asyncUpdates = true;
|
||||
render() {
|
||||
return this.props.children;
|
||||
}
|
||||
}
|
||||
ReactDOM.render(<Async><div>Hi</div></Async>, container);
|
||||
expect(container.textContent).toEqual('');
|
||||
jest.runAllTimers();
|
||||
expect(container.textContent).toEqual('Hi');
|
||||
|
||||
ReactDOM.render(<Async><div>Bye</div></Async>, container);
|
||||
expect(container.textContent).toEqual('Hi');
|
||||
jest.runAllTimers();
|
||||
expect(container.textContent).toEqual('Bye');
|
||||
});
|
||||
|
||||
it('updates inside an async tree are async by default', () => {
|
||||
class Async extends React.Component {
|
||||
static unstable_asyncUpdates = true;
|
||||
render() {
|
||||
return this.props.children;
|
||||
}
|
||||
}
|
||||
let instance;
|
||||
class Component extends React.Component {
|
||||
state = {step: 0};
|
||||
render() {
|
||||
instance = this;
|
||||
return <div>{this.state.step}</div>;
|
||||
}
|
||||
}
|
||||
|
||||
ReactDOM.render(<Async><Component /></Async>, container);
|
||||
expect(container.textContent).toEqual('');
|
||||
jest.runAllTimers();
|
||||
expect(container.textContent).toEqual('0');
|
||||
|
||||
instance.setState({step: 1});
|
||||
expect(container.textContent).toEqual('0');
|
||||
jest.runAllTimers();
|
||||
expect(container.textContent).toEqual('1');
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -66,7 +66,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
|
||||
config: HostConfig<T, P, I, TI, PI, C, CX, PL>,
|
||||
hostContext: HostContext<C, CX>,
|
||||
scheduleUpdate: (fiber: Fiber, priorityLevel: PriorityLevel) => void,
|
||||
getPriorityContext: (fiber: Fiber) => PriorityLevel,
|
||||
getPriorityContext: (fiber: Fiber, forceAsync: boolean) => PriorityLevel,
|
||||
) {
|
||||
const {
|
||||
shouldSetTextContent,
|
||||
|
||||
@@ -60,7 +60,7 @@ if (__DEV__) {
|
||||
|
||||
module.exports = function(
|
||||
scheduleUpdate: (fiber: Fiber, priorityLevel: PriorityLevel) => void,
|
||||
getPriorityContext: (fiber: Fiber) => PriorityLevel,
|
||||
getPriorityContext: (fiber: Fiber, forceAsync: boolean) => PriorityLevel,
|
||||
memoizeProps: (workInProgress: Fiber, props: any) => void,
|
||||
memoizeState: (workInProgress: Fiber, state: any) => void,
|
||||
) {
|
||||
@@ -69,7 +69,7 @@ module.exports = function(
|
||||
isMounted,
|
||||
enqueueSetState(instance, partialState, callback) {
|
||||
const fiber = ReactInstanceMap.get(instance);
|
||||
const priorityLevel = getPriorityContext(fiber);
|
||||
const priorityLevel = getPriorityContext(fiber, false);
|
||||
callback = callback === undefined ? null : callback;
|
||||
if (__DEV__) {
|
||||
warnOnInvalidCallback(callback, 'setState');
|
||||
@@ -79,7 +79,7 @@ module.exports = function(
|
||||
},
|
||||
enqueueReplaceState(instance, state, callback) {
|
||||
const fiber = ReactInstanceMap.get(instance);
|
||||
const priorityLevel = getPriorityContext(fiber);
|
||||
const priorityLevel = getPriorityContext(fiber, false);
|
||||
callback = callback === undefined ? null : callback;
|
||||
if (__DEV__) {
|
||||
warnOnInvalidCallback(callback, 'replaceState');
|
||||
@@ -89,7 +89,7 @@ module.exports = function(
|
||||
},
|
||||
enqueueForceUpdate(instance, callback) {
|
||||
const fiber = ReactInstanceMap.get(instance);
|
||||
const priorityLevel = getPriorityContext(fiber);
|
||||
const priorityLevel = getPriorityContext(fiber, false);
|
||||
callback = callback === undefined ? null : callback;
|
||||
if (__DEV__) {
|
||||
warnOnInvalidCallback(callback, 'forceUpdate');
|
||||
@@ -308,7 +308,7 @@ module.exports = function(
|
||||
instance.refs = emptyObject;
|
||||
instance.context = getMaskedContext(workInProgress, unmaskedContext);
|
||||
|
||||
if (instance.unstable_asyncUpdates === true) {
|
||||
if (workInProgress.type.unstable_asyncUpdates === true) {
|
||||
workInProgress.internalContextTag |= AsyncUpdates;
|
||||
}
|
||||
|
||||
|
||||
@@ -124,19 +124,12 @@ export type HostConfig<T, P, I, TI, PI, C, CX, PL> = {
|
||||
|
||||
export type Reconciler<C, I, TI> = {
|
||||
createContainer(containerInfo: C): OpaqueRoot,
|
||||
createAsyncContainer(containerInfo: C): OpaqueRoot,
|
||||
updateContainer(
|
||||
element: ReactNodeList,
|
||||
container: OpaqueRoot,
|
||||
parentComponent: ?ReactComponent<any, any, any>,
|
||||
callback: ?Function,
|
||||
): void,
|
||||
updateAsyncContainer(
|
||||
element: ReactNodeList,
|
||||
container: OpaqueRoot,
|
||||
parentComponent: ?ReactComponent<any, any, any>,
|
||||
callback: ?Function,
|
||||
): void,
|
||||
performWithPriority(priorityLevel: PriorityLevel, fn: Function): void,
|
||||
batchedUpdates<A>(fn: () => A): A,
|
||||
unbatchedUpdates<A>(fn: () => A): A,
|
||||
@@ -193,7 +186,13 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
|
||||
}
|
||||
}
|
||||
|
||||
const priorityLevel = getPriorityContext(current);
|
||||
// Check if the top-level element is an async wrapper component. If so, treat
|
||||
// updates to the root as async. This is a bit weird but lets us avoid a separate
|
||||
// `renderAsync` API.
|
||||
const forceAsync = element != null &&
|
||||
element.type != null &&
|
||||
(element.type: any).unstable_asyncUpdates === true;
|
||||
const priorityLevel = getPriorityContext(current, forceAsync);
|
||||
const nextState = {element};
|
||||
callback = callback === undefined ? null : callback;
|
||||
if (__DEV__) {
|
||||
@@ -208,85 +207,40 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
|
||||
scheduleUpdate(current, priorityLevel);
|
||||
}
|
||||
|
||||
function updateContainer(
|
||||
element: ReactNodeList,
|
||||
container: OpaqueRoot,
|
||||
parentComponent: ?ReactComponent<any, any, any>,
|
||||
async: boolean,
|
||||
callback: ?Function,
|
||||
) {
|
||||
// TODO: Make messages more user-friendly?
|
||||
if (__DEV__) {
|
||||
warning(
|
||||
!async || container.current.internalContextTag & AsyncUpdates,
|
||||
'Attempted to schedule an asynchronous update on a sync container.',
|
||||
);
|
||||
}
|
||||
|
||||
// TODO: If this is a nested container, this won't be the root.
|
||||
const current = container.current;
|
||||
|
||||
if (__DEV__) {
|
||||
if (ReactFiberInstrumentation.debugTool) {
|
||||
if (current.alternate === null) {
|
||||
ReactFiberInstrumentation.debugTool.onMountContainer(container);
|
||||
} else if (element === null) {
|
||||
ReactFiberInstrumentation.debugTool.onUnmountContainer(container);
|
||||
} else {
|
||||
ReactFiberInstrumentation.debugTool.onUpdateContainer(container);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const context = getContextForSubtree(parentComponent);
|
||||
if (container.context === null) {
|
||||
container.context = context;
|
||||
} else {
|
||||
container.pendingContext = context;
|
||||
}
|
||||
|
||||
scheduleTopLevelUpdate(current, element, callback);
|
||||
}
|
||||
|
||||
return {
|
||||
createContainer(containerInfo: C): OpaqueRoot {
|
||||
return createFiberRoot(containerInfo);
|
||||
},
|
||||
|
||||
createAsyncContainer(containerInfo: C): OpaqueRoot {
|
||||
const fiberRoot = createFiberRoot(containerInfo);
|
||||
fiberRoot.current.internalContextTag |= AsyncUpdates;
|
||||
return fiberRoot;
|
||||
},
|
||||
|
||||
updateContainer(
|
||||
element: ReactNodeList,
|
||||
container: OpaqueRoot,
|
||||
parentComponent: ?ReactComponent<any, any, any>,
|
||||
callback: ?Function,
|
||||
): void {
|
||||
updateContainer(
|
||||
element,
|
||||
container,
|
||||
parentComponent,
|
||||
false, // async = false
|
||||
callback,
|
||||
);
|
||||
},
|
||||
// TODO: If this is a nested container, this won't be the root.
|
||||
const current = container.current;
|
||||
|
||||
updateAsyncContainer(
|
||||
element: ReactNodeList,
|
||||
container: OpaqueRoot,
|
||||
parentComponent: ?ReactComponent<any, any, any>,
|
||||
callback: ?Function,
|
||||
) {
|
||||
updateContainer(
|
||||
element,
|
||||
container,
|
||||
parentComponent,
|
||||
true, // async = true
|
||||
callback,
|
||||
);
|
||||
if (__DEV__) {
|
||||
if (ReactFiberInstrumentation.debugTool) {
|
||||
if (current.alternate === null) {
|
||||
ReactFiberInstrumentation.debugTool.onMountContainer(container);
|
||||
} else if (element === null) {
|
||||
ReactFiberInstrumentation.debugTool.onUnmountContainer(container);
|
||||
} else {
|
||||
ReactFiberInstrumentation.debugTool.onUpdateContainer(container);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const context = getContextForSubtree(parentComponent);
|
||||
if (container.context === null) {
|
||||
container.context = context;
|
||||
} else {
|
||||
container.pendingContext = context;
|
||||
}
|
||||
|
||||
scheduleTopLevelUpdate(current, element, callback);
|
||||
},
|
||||
|
||||
performWithPriority,
|
||||
|
||||
@@ -1345,12 +1345,17 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
|
||||
}
|
||||
}
|
||||
|
||||
function getPriorityContext(fiber: Fiber): PriorityLevel {
|
||||
function getPriorityContext(
|
||||
fiber: Fiber,
|
||||
forceAsync: boolean,
|
||||
): PriorityLevel {
|
||||
let priorityLevel = priorityContext;
|
||||
if (priorityLevel === NoWork) {
|
||||
if (
|
||||
!useSyncScheduling ||
|
||||
(enableAsyncSubtreeAPI === true && fiber.internalContextTag & AsyncUpdates)
|
||||
(enableAsyncSubtreeAPI === true &&
|
||||
fiber.internalContextTag & AsyncUpdates) ||
|
||||
forceAsync
|
||||
) {
|
||||
priorityLevel = LowPriority;
|
||||
} else {
|
||||
|
||||
Reference in new issue
Block a user