Skip to content

Commit

Permalink
Renderer track if element is root
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Jan 21, 2019
1 parent f0870bb commit 302a706
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions lib/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,37 @@ class PartialRenderer extends ReactDOMServerRenderer {

this._interrupts = [];

const isRootElement = stackState ? stackState.isRootElement : true;
this.isRootElement = isRootElement;

if (!stackState) return;

// Not root node.
// Add frame to stack to reinstate stack state from parent render
// and avoid `data-reactroot=""` markup on first element.
// Nested render
// Reinstate stack state from parent render.
// If not root element, add frame to stack to avoid `data-reactroot=""`
// markup on first element.
const {stack} = this;
const topFrame = stack[0];

const frame = {
type: null,
domNamespace: stackState.domNamespace,
children: topFrame.children,
childIndex: 0,
// Restore contexts from legacy Context API
context: stackState.context,
footer: ''
};
if (topFrame.debugElementStack) frame.debugElementStack = [];

stack[1] = frame;
topFrame.children = [];
if (isRootElement) {
// Add state to existing frame on stack
topFrame.domNamespace = stackState.domNamespace;
topFrame.context = stackState.context;
} else {
// Add frame to stack
const frame = {
type: null,
domNamespace: stackState.domNamespace,
children: topFrame.children,
childIndex: 0,
context: stackState.context,
footer: ''
};
if (topFrame.debugElementStack) frame.debugElementStack = [];

stack[1] = frame;
topFrame.children = [];
}

// Restore contexts from new Context API
const {threadID} = this;
Expand Down Expand Up @@ -81,7 +91,8 @@ class PartialRenderer extends ReactDOMServerRenderer {
// Capture contexts from legacy Context API
context,
contexts,
domNamespace
domNamespace,
isRootElement: this.isRootElement && this.stack.length === 1
}
};
if (props) Object.assign(interrupt, props);
Expand Down

0 comments on commit 302a706

Please sign in to comment.