Skip to content

Commit eb754bd

Browse files
yusukebeashunar0
andauthored
fix(jsx): do not render ref objects as attributes (#5615)
Co-authored-by: Ashunar0 <[email protected]>
1 parent 6cbe361 commit eb754bd

3 files changed

Lines changed: 14 additions & 2 deletions

File tree

‎src/jsx/base.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ export const attributeToBuffer = (
165165
v: unknown,
166166
tag?: string
167167
): void => {
168-
if (v === null || v === undefined) {
168+
// refs only make sense in the DOM
169+
if (key === 'ref' || v === null || v === undefined) {
169170
return
170171
}
171172
if (typeof v === 'boolean' && booleanAttributes.has(key)) {
@@ -175,7 +176,7 @@ export const attributeToBuffer = (
175176
return
176177
}
177178
if (typeof v === 'function') {
178-
if (!key.startsWith('on') && key !== 'ref') {
179+
if (!key.startsWith('on')) {
179180
throw new Error(
180181
`Invalid prop '${key}' of type 'function' supplied to ${tag ? `'${tag}'` : 'a JSX element'}.`
181182
)

‎src/jsx/index.test.tsx‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,12 @@ describe('render to string', () => {
482482
expect(template.toString()).toBe('<div>Content</div>')
483483
})
484484

485+
it('should be ignored used in ref props with a ref object', () => {
486+
const ref = { current: null }
487+
const template = <div ref={ref}>Content</div>
488+
expect(template.toString()).toBe('<div>Content</div>')
489+
})
490+
485491
it('should raise an error if used in other props', () => {
486492
const onClick = () => {}
487493
const template = <button data-handler={onClick}>Click</button>

‎src/jsx/jsx-runtime.test.tsx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ describe('jsx-runtime', () => {
5050
)
5151
})
5252

53+
it('Should render nothing for a ref in jsxAttr()', () => {
54+
expect(String(jsxAttr('ref', { current: null } as never))).toBe('')
55+
expect(String(jsxAttr('ref', (() => {}) as never))).toBe('')
56+
})
57+
5358
it('Should render nothing for a null style object in jsxAttr()', () => {
5459
expect(String(jsxAttr('style', null as never))).toBe('')
5560
})

0 commit comments

Comments
 (0)