Shouldn’t multiple root elements become a fragment?
See original GitHub issueThis 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:
- Created 3 years ago
- Reactions:6
- Comments:8 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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.Best fix at the moment is to either:
<div>
(less than ideal when semantic structure matters)<${React.Fragment}>
Being able to tersely type
<>...</>
, as JSX allows, would be a nice enhancement.