From 4ce5da7aee90a373f2f36d1beb559097af30952e Mon Sep 17 00:00:00 2001 From: Clement Hoang Date: Mon, 30 Oct 2017 17:52:40 -0700 Subject: [PATCH] Add Fragment as a named export to React (#10783) * Add Fragment as a named export to React * Remove extra tests for Fragment * Change React.Fragment export to be a string '#fragment' * Fix fragment special case to work with 1 child * Add single child test for fragment export * Move fragment definition to ReactEntry.js and render components for key warning tests * Inline createFiberFromElementType into createFiberFromElement * Update reconciliation to special case fragments * Use same semantics as implicit childsets for ReactFragment * Add more fragment state preservation tests * Export symbol instead of string for fragments * Fix rebase breakages * Re-apply prettier at 1.2.2 * Merge branches in updateElement * Remove unnecessary check * Re-use createFiberFromFragment for fragment case * Simplyify branches by adding type field to fragment fiber * Move branching logic for fragments to broader methods when possible. * Add more tests for fragments * Address Dan's feedback * Move REACT_FRAGMENT_TYPE into __DEV__ block for DCE * Change hex representation of REACT_FRAGMENT_TYPE to follow convention * Remove unnecessary branching and isArray checks * Update test for preserving children state when keys are same * Fix updateSlot bug and add more tests * Make fragment tests more robust by using ops pattern * Update jsx element validator to allow numbers and symbols * Remove type field from fragment fiber * Fork reconcileChildFibers instead of recursing * Use ternary if condition * Revamp fragment test suite: - Add more coverage to fragment tests - Use better names - Remove useless Fragment component inside tests - Remove useless tests so that tests are more concise * Check output of renderer in fragment tests to ensure no silly business despite states being preserved * Finish implementation of fragment reconciliation with desired behavior * Add reverse render direction for fragment tests * Remove unneeded fragment branch in updateElement * Add more test cases for ReactFragment * Handle childless fragment in reconciler * Support fragment flattening in SSR * Clean up ReactPartialRenderer * Warn when non-key and children props are passed to fragments * Add non-null key check back to updateSlot's array's case * Add test for positional reconciliation in fragments * Add warning for refs in fragments with stack trace --- .../ReactDOMServerIntegration-test.js | 50 ++ .../src/server/ReactPartialRenderer.js | 86 ++- packages/react-noop-renderer/src/ReactNoop.js | 3 +- .../react-reconciler/src/ReactChildFiber.js | 136 +++- packages/react-reconciler/src/ReactFiber.js | 86 +-- .../src/__tests__/ReactFragment-test.js | 721 ++++++++++++++++++ packages/react/src/React.js | 7 + packages/react/src/ReactElementValidator.js | 53 +- .../__tests__/ReactElementValidator-test.js | 12 +- .../ReactJSXElementValidator-test.js | 115 ++- 10 files changed, 1120 insertions(+), 149 deletions(-) create mode 100644 packages/react-reconciler/src/__tests__/ReactFragment-test.js diff --git a/packages/react-dom/src/__tests__/ReactDOMServerIntegration-test.js b/packages/react-dom/src/__tests__/ReactDOMServerIntegration-test.js index 06d8a0eef1..4d09d2e04a 100644 --- a/packages/react-dom/src/__tests__/ReactDOMServerIntegration-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMServerIntegration-test.js @@ -403,6 +403,55 @@ describe('ReactDOMServerIntegration', () => { expect(parent.childNodes[2].tagName).toBe('P'); }); + itRenders('a fragment with one child', async render => { + let e = await render(
text1
); + let parent = e.parentNode; + expect(parent.childNodes[0].tagName).toBe('DIV'); + }); + + itRenders('a fragment with several children', async render => { + let Header = props => { + return

header

; + }; + let Footer = props => { + return

footer

about

; + }; + let e = await render( + +
text1
+ text2 +
+