question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Shouldn’t multiple root elements become a fragment?

See original GitHub issue

This works for me:

  return html`
    <${React.Fragment}>
      <h1>Quiz</h1>
      ${entriesUi}
      <hr />
      <${Summary} entries=${entries} />
    <//>
  `;

If I don’t wrap the fragment around the roots, I’m getting this error:

Warning: Each child in a list should have a unique “key” prop.

(That is, HTM seems to return an Array and not a fragment.)

If my suggestion doesn’t make sense, then it may be helpful to support the <>...</> syntax for fragments.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:6
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
bathoscommented, Aug 27, 2020

This threw me off too. I was expecting createElement(Fragment, {}, ...nodes) treatment for cases like this given the positions will be static and keying is known to be unnecessary up-front.

I’d actually thought that this was one of the main selling-points of htm — that the explicit boundaries of the template obviated the need for explicit fragments within the string content — but it seems I misinterpreted the meaning of the “Multiple root element (fragments)” bullet point in the readme section describing improvements over JSX.

1reaction
jimnielscommented, Nov 18, 2020

Was the main issue here that React displayed the questionable warning, or is there a case I’m not aware of where this actually prevents usage?

For me personally, it was that React keeps displaying a warning. Because htm makes multiple root elements so easy, I do it in many components. However, that leads to react throwing lots of warnings in the console, which makes finding things I’ve actually done wrong more difficult.

Screen Shot 2020-11-18 at 2 18 20 PM

Best fix at the moment is to either:

  • nest every component in a root <div> (less than ideal when semantic structure matters)
  • use @rauschma 's suggestion of <${React.Fragment}>

Being able to tersely type <>...</>, as JSX allows, would be a nice enhancement.

Read more comments on GitHub >

github_iconTop Results From Across the Web

XML fragment with multiple roots: suppressing VS error
XML Documents have only a single root, there is no such thing as a "xml fragment document". Two options: Use two files, one...
Read more >
Can A Vue Template Have Multiple Root Nodes (Fragments)?
Rendering a list of child nodes in a component is not a design pattern that works with Vue due to the single root...
Read more >
XML Fragment Interchange - W3C
The challenge is that an isolated element from an XML document may not contain quite enough information to be parsed correctly.
Read more >
createRoot - React Docs
createRoot lets you create a root to display React components inside a browser DOM ... Useful to avoid conflicts when using multiple roots...
Read more >
its return type 'element | element[]' is not a valid jsx element. - You ...
That fragment needs to be the single root element. ... You nest all returned JSX in one single fragment. ... You shouldn't ever...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found