Skip to content

Commit cc05806

Browse files
committed
Fix to not pass children for void elements
React warns when `children: []` is passed for a void element, such as `hr` or `br`. To solve that, we can pass `children: undefined` instead.
1 parent 94b2a72 commit cc05806

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

Diff for: lib/index.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
* @typedef {JSX.Element | string | null | undefined} Child
6868
* Child.
6969
*
70-
* @typedef {{children: Array<Child>, node?: Element | undefined, [prop: string]: Value | Element | undefined | Array<Child>}} Props
70+
* @typedef {{children: Array<Child> | undefined, node?: Element | undefined, [prop: string]: Value | Element | undefined | Array<Child>}} Props
7171
* Properties and children.
7272
*
7373
* @callback Create
@@ -260,7 +260,7 @@ export function toJsxRuntime(tree, options) {
260260
return state.create(
261261
tree,
262262
state.Fragment,
263-
{children: result ? [result] : []},
263+
{children: result ? [result] : undefined},
264264
undefined
265265
)
266266
}
@@ -296,7 +296,7 @@ function one(state, node, key) {
296296
let type = state.Fragment
297297

298298
if (node.type === 'element') {
299-
if (tableElements.has(node.tagName)) {
299+
if (children && tableElements.has(node.tagName)) {
300300
children = children.filter((child) => !whitespace(child))
301301
}
302302

@@ -340,7 +340,7 @@ function productionCreate(_, jsx, jsxs) {
340340
return create
341341
/** @type {Create} */
342342
function create(_, type, props, key) {
343-
const isStaticChildren = props.children.length > 1
343+
const isStaticChildren = props.children ? props.children.length > 1 : false
344344
const fn = isStaticChildren ? jsxs : jsx
345345
return fn(type, props, key)
346346
}
@@ -358,7 +358,7 @@ function developmentCreate(filePath, jsxDEV) {
358358
return create
359359
/** @type {Create} */
360360
function create(node, type, props, key) {
361-
const isStaticChildren = props.children.length > 1
361+
const isStaticChildren = props.children ? props.children.length > 1 : false
362362
const point = pointStart(node)
363363
return jsxDEV(
364364
type,
@@ -382,7 +382,7 @@ function developmentCreate(filePath, jsxDEV) {
382382
* Info passed around.
383383
* @param {Parent} node
384384
* Current element.
385-
* @returns {Array<Child>}
385+
* @returns {Array<Child> | undefined}
386386
* Children.
387387
*/
388388
function createChildren(state, node) {
@@ -407,7 +407,7 @@ function createChildren(state, node) {
407407
if (result !== undefined) children.push(result)
408408
}
409409

410-
return children
410+
return children.length > 0 ? children : undefined
411411
}
412412

413413
/**

Diff for: test/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@ test('children', () => {
292292
'<div class="article"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 500 500"><circle cx="120" cy="120" r="100"></circle></svg></div>',
293293
'should support svg in html'
294294
)
295+
296+
assert.equal(
297+
renderToStaticMarkup(toJsxRuntime(h('hr'), production)),
298+
'<hr/>',
299+
'should support a void element'
300+
)
295301
})
296302

297303
test('source', () => {

0 commit comments

Comments
 (0)