Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternate fix for "Fix a crash in Suspense with findDOMNode" #15312

Merged
merged 2 commits into from
Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,48 @@ describe('ReactDOMSuspensePlaceholder', () => {
await Lazy;
expect(log).toEqual(['cDU first', 'cDU second']);
});

// Regression test for https://github.com/facebook/react/issues/14188
it('can call findDOMNode() in a suspended component commit phase (#2)', () => {
let suspendOnce = Promise.resolve();
function Suspend() {
if (suspendOnce) {
let promise = suspendOnce;
suspendOnce = null;
throw promise;
}
return null;
}

const log = [];
class Child extends React.Component {
componentDidMount() {
log.push('cDM');
ReactDOM.findDOMNode(this);
}

componentDidUpdate() {
log.push('cDU');
ReactDOM.findDOMNode(this);
}

render() {
return null;
}
}

function App() {
return (
<Suspense fallback="Loading">
<Suspend />
<Child />
</Suspense>
);
}

ReactDOM.render(<App />, container);
expect(log).toEqual(['cDM']);
ReactDOM.render(<App />, container);
expect(log).toEqual(['cDM', 'cDU']);
});
});
21 changes: 17 additions & 4 deletions packages/react-reconciler/src/ReactFiberTreeReflection.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,28 @@ export function findCurrentFiberUsingSlowPath(fiber: Fiber): Fiber | null {
// If we have two possible branches, we'll walk backwards up to the root
// to see what path the root points to. On the way we may hit one of the
// special cases and we'll deal with them.
let a = fiber;
let b = alternate;
let a: Fiber = fiber;
let b: Fiber = alternate;
while (true) {
let parentA = a.return;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if parentA here is a Suspense boundary with no fragment and its alternate is a Suspense boundary with a fragment?

In that case the children won't be representing the same thing. However, they also will never be equal so maybe it doesn't matter?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was my thinking, at least

let parentB = parentA ? parentA.alternate : null;
if (!parentA || !parentB) {
if (parentA === null) {
// We're at the root.
break;
}
let parentB = parentA.alternate;
if (parentB === null) {
// There is no alternate. This is an unusual case. Currently, it only
// happens when a Suspense component is hidden. An extra fragment fiber
// is inserted in between the Suspense fiber and its children. Skip
// over this extra fragment fiber and proceed to the next parent.
const nextParent = parentA.return;
if (nextParent !== null) {
a = b = nextParent;
continue;
}
// If there's no parent, we're at the root.
break;
}

// If both copies of the parent fiber point to the same child, we can
// assume that the child is current. This happens when we bailout on low
Expand Down