-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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
fix renderToString fails with array type children when react-dom/server render #10221
Changes from all commits
790e65a
c2aeb06
6fd78e5
4b28bc1
f652ed7
322fa40
deb11af
62c042e
6b4eb0e
e53e552
eafb4bd
3df964d
6f7f142
02101d5
d661c5a
97575fd
e0bd356
1d45bae
5f8a2dd
cf005dd
7f5c0a1
60e7e3e
e4dd558
59e0843
5539f09
06d9971
b42dd08
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -332,6 +332,19 @@ describe('ReactDOMServerIntegration', () => { | |
}); | ||
|
||
describe('basic rendering', function() { | ||
beforeEach(() => { | ||
onAfterResetModules = () => { | ||
const ReactFeatureFlags = require('ReactFeatureFlags'); | ||
ReactFeatureFlags.disableNewFiberFeatures = false; | ||
}; | ||
|
||
resetModules(); | ||
}); | ||
|
||
afterEach(() => { | ||
onAfterResetModules = null; | ||
}); | ||
|
||
itRenders('a blank div', async render => { | ||
const e = await render(<div />); | ||
expect(e.tagName).toBe('DIV'); | ||
|
@@ -347,6 +360,70 @@ describe('ReactDOMServerIntegration', () => { | |
expect(e.childNodes.length).toBe(1); | ||
expect(e.firstChild.tagName).toBe('BR'); | ||
}); | ||
|
||
if (ReactDOMFeatureFlags.useFiber) { | ||
itRenders('a array type children as a child', async render => { | ||
let Header = props => { | ||
return <p>header</p>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's also make the component return array as well. This way we verify it's supported at multiple levels rather than just top level. |
||
}; | ||
let Footer = props => { | ||
return [<h2 key={1}>footer</h2>, <h3 key={2}>about</h3>]; | ||
}; | ||
let e = await render([ | ||
<div key={1}>text1</div>, | ||
<span key={2}>text2</span>, | ||
<Header key={3} />, | ||
<Footer key={4} />, | ||
]); | ||
let parent = e.parentNode; | ||
expect(parent.childNodes[0].tagName).toBe('DIV'); | ||
expect(parent.childNodes[1].tagName).toBe('SPAN'); | ||
expect(parent.childNodes[2].tagName).toBe('P'); | ||
expect(parent.childNodes[3].tagName).toBe('H2'); | ||
expect(parent.childNodes[4].tagName).toBe('H3'); | ||
}); | ||
|
||
itRenders('a single array element children as a child', async render => { | ||
let e = await render([<div key={1}>text1</div>]); | ||
let parent = e.parentNode; | ||
expect(parent.childNodes[0].tagName).toBe('DIV'); | ||
}); | ||
|
||
itRenders('a nested array children as a child', async render => { | ||
let e = await render([ | ||
[<div key={1}>text1</div>], | ||
<span key={1}>text2</span>, | ||
[[[null, <p key={1} />], false]], | ||
]); | ||
let parent = e.parentNode; | ||
expect(parent.childNodes[0].tagName).toBe('DIV'); | ||
expect(parent.childNodes[1].tagName).toBe('SPAN'); | ||
expect(parent.childNodes[2].tagName).toBe('P'); | ||
}); | ||
|
||
itRenders('an iterable as a child', async render => { | ||
const threeDivIterable = { | ||
'@@iterator': function() { | ||
var i = 0; | ||
return { | ||
next: function() { | ||
if (i++ < 3) { | ||
return {value: <div key={i} />, done: false}; | ||
} else { | ||
return {value: undefined, done: true}; | ||
} | ||
}, | ||
}; | ||
}, | ||
}; | ||
let e = await render(threeDivIterable); | ||
let parent = e.parentNode; | ||
expect(parent.childNodes.length).toBe(3); | ||
expect(parent.childNodes[0].tagName).toBe('DIV'); | ||
expect(parent.childNodes[1].tagName).toBe('DIV'); | ||
expect(parent.childNodes[2].tagName).toBe('DIV'); | ||
}); | ||
} | ||
}); | ||
|
||
describe('property to attribute mapping', function() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -307,11 +307,6 @@ function createOpenTagMarkup( | |
} | ||
|
||
function resolve(child, context) { | ||
// TODO: We'll need to support Arrays (and strings) after Fiber is rolled out | ||
invariant( | ||
!Array.isArray(child), | ||
'The server renderer does not implement support for array children yet.', | ||
); | ||
while (React.isValidElement(child)) { | ||
if (__DEV__) { | ||
pushElementToDebugStack(child); | ||
|
@@ -418,8 +413,9 @@ function resolve(child, context) { | |
|
||
class ReactDOMServerRenderer { | ||
constructor(element, makeStaticMarkup) { | ||
var children = React.isValidElement(element) ? [element] : toArray(element); | ||
var topFrame = { | ||
children: [element], | ||
children, | ||
childIndex: 0, | ||
context: emptyObject, | ||
footer: '', | ||
|
@@ -487,7 +483,22 @@ class ReactDOMServerRenderer { | |
if (child === null || child === false) { | ||
return ''; | ||
} else { | ||
return this.renderDOM(child, context); | ||
if (React.isValidElement(child)) { | ||
return this.renderDOM(child, context); | ||
} else { | ||
var children = toArray(child); | ||
var frame = { | ||
children, | ||
childIndex: 0, | ||
context: context, | ||
footer: '', | ||
}; | ||
if (__DEV__) { | ||
frame.debugElementStack = []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need to do this? (Note: I'm not very familiar with this code so I'm learning it too.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "setCurrentDebugStack" method will set debugElementStack.length = 1, This is to init the debugElementStack array. The same method handles when HostCompoment return array type children.https://github.com/facebook/react/blob/master/src/renderers/shared/server/ReactPartialRenderer.js#L769 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do warnings still work? You can add a test similar to this one but using an array. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gaearo, It's done. Please review. |
||
} | ||
this.stack.push(frame); | ||
return ''; | ||
} | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still seems too early. It only validates top-level element, but not its children. What I was referring to is that we have this validation in PartialRenderer and it should probably throw in some cases otherwise. Validation should happen as we
resolve
children rather than at the top.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😃 I understand what you mean. I fell that children do not need validation. Because when resolve method meet array type children , It will return to render method, The render method will check if child is a array type, If it is then push to stack. So resolve method only validates single element child and do not validates array type children.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, but don't we still want to throw if it's neither array nor valid element?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use 'React.children.toArray', It will accept valid element or valid array, otherwise it will throw an error.
React.isValidElement
.React.isValidElement
.So we can validated the following cases by
React.children.toArray
:ReactDOMServer.renderToString(reactElement)