mirror of
https://github.com/facebook/react.git
synced 2026-11-18 20:15:56 +00:00
When a reconciliation gets down prioritized, reuse children
When we downprioritize children we need to remember to reuse the old children in the update side-effect. This whole set up is very unfortunate since we can have children in our active tree that never actually finished rendering. This strategy might be fundamentally flawed, not sure.
This commit is contained in:
1 parent
d0838797a3
commit
5ac87e0d0f
5 files changed
+64
-3
No files matched your search
@@ -106,6 +106,11 @@ export type Fiber = Instance & {
|
||||
// I'm not really sure so I'll use a flag for now.
|
||||
// TODO: Find another way to infer this flag.
|
||||
hasWorkInProgress: bool,
|
||||
// Keeps track if we've deprioritized the children of this node so we know to
|
||||
// reuse the existing children.
|
||||
// TODO: Find another way to infer this flag or separate children updates from
|
||||
// property updates so that we simply don't try to update children here.
|
||||
wasDeprioritized: bool,
|
||||
|
||||
// Conceptual aliases
|
||||
// workInProgress : Fiber -> alternate The alternate used for reuse happens
|
||||
@@ -146,6 +151,7 @@ var createFiber = function(tag : TypeOfWork, key : null | string) : Fiber {
|
||||
pendingWorkPriority: NoWork,
|
||||
|
||||
hasWorkInProgress: false,
|
||||
wasDeprioritized: false,
|
||||
|
||||
alternate: null,
|
||||
|
||||
|
||||
@@ -82,6 +82,7 @@ module.exports = function<T, P, I, C>(config : HostConfig<T, P, I, C>) {
|
||||
OffscreenPriority
|
||||
);
|
||||
workInProgress.pendingWorkPriority = OffscreenPriority;
|
||||
workInProgress.wasDeprioritized = true;
|
||||
return null;
|
||||
} else {
|
||||
workInProgress.child = reconcileChildFibers(
|
||||
@@ -91,6 +92,7 @@ module.exports = function<T, P, I, C>(config : HostConfig<T, P, I, C>) {
|
||||
priority
|
||||
);
|
||||
workInProgress.pendingWorkPriority = NoWork;
|
||||
workInProgress.wasDeprioritized = false;
|
||||
return workInProgress.child;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +47,14 @@ module.exports = function<T, P, I, C>(config : HostConfig<T, P, I, C>) {
|
||||
throw new Error('This should only be done during updates.');
|
||||
}
|
||||
// Commit the work prepared earlier.
|
||||
const child = (finishedWork.child : ?Fiber);
|
||||
let child;
|
||||
if (finishedWork.wasDeprioritized) {
|
||||
// If this was a down priority, we need to preserve the old child in
|
||||
// the output.
|
||||
child = finishedWork.alternate ? finishedWork.alternate.child : null;
|
||||
} else {
|
||||
child = finishedWork.child;
|
||||
}
|
||||
const children = (child && !child.sibling) ? (child.output : ?Fiber | I) : child;
|
||||
const newProps = finishedWork.memoizedProps;
|
||||
const current = finishedWork.alternate;
|
||||
|
||||
@@ -140,9 +140,16 @@ module.exports = function<T, P, I, C>(config : HostConfig<T, P, I, C>) {
|
||||
markForPreEffect(workInProgress);
|
||||
return null;
|
||||
case HostComponent:
|
||||
const child = workInProgress.child;
|
||||
const children = (child && !child.sibling) ? (child.output : ?Fiber | I) : child;
|
||||
let child;
|
||||
let newProps = workInProgress.pendingProps;
|
||||
if (workInProgress.wasDeprioritized) {
|
||||
// If this was a down priority, we need to preserve the old child in
|
||||
// the output.
|
||||
child = current ? current.child : null;
|
||||
} else {
|
||||
child = workInProgress.child;
|
||||
}
|
||||
const children = (child && !child.sibling) ? (child.output : ?Fiber | I) : child;
|
||||
if (current && workInProgress.stateNode != null) {
|
||||
// If we have an alternate, that means this is an update and we need to
|
||||
// schedule a side-effect to do the updates.
|
||||
|
||||
@@ -89,6 +89,45 @@ describe('ReactIncremental', function() {
|
||||
|
||||
});
|
||||
|
||||
it('preserves a previously rendered node when deprioritized', function() {
|
||||
|
||||
function Middle(props) {
|
||||
return <span prop={props.children} />;
|
||||
}
|
||||
|
||||
function Foo(props) {
|
||||
return (
|
||||
<div>
|
||||
<div hidden={true}>
|
||||
<Middle>{props.text}</Middle>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
ReactNoop.render(<Foo text="foo" />);
|
||||
ReactNoop.flush();
|
||||
|
||||
expect(ReactNoop.root.children).toEqual([
|
||||
div(div(span('foo'))),
|
||||
]);
|
||||
|
||||
ReactNoop.render(<Foo text="bar" />);
|
||||
ReactNoop.flushLowPri(20);
|
||||
|
||||
expect(ReactNoop.root.children).toEqual([
|
||||
div(div(span('foo'))),
|
||||
]);
|
||||
|
||||
ReactNoop.flush();
|
||||
|
||||
expect(ReactNoop.root.children).toEqual([
|
||||
div(div(span('bar'))),
|
||||
]);
|
||||
|
||||
});
|
||||
|
||||
|
||||
it('updates a child even though the old props is empty', function() {
|
||||
function Foo(props) {
|
||||
return (
|
||||
|
||||
Reference in new issue
Block a user