diff --git a/jsx-runtime/src/index.js b/jsx-runtime/src/index.js index 8cbe9ea444..144ac83712 100644 --- a/jsx-runtime/src/index.js +++ b/jsx-runtime/src/index.js @@ -27,17 +27,22 @@ const isArray = Array.isArray; * @param {unknown} [__self] */ function createVNode(type, props, key, isStaticChildren, __source, __self) { + if (!props) props = {}; // We'll want to preserve `ref` in props to get rid of the need for // forwardRef components in the future, but that should happen via // a separate PR. - let normalizedProps = {}, + let normalizedProps = props, ref, i; - for (i in props) { - if (i == 'ref') { - ref = props[i]; - } else { - normalizedProps[i] = props[i]; + + if ('ref' in normalizedProps) { + normalizedProps = {}; + for (i in props) { + if (i == 'ref') { + ref = props[i]; + } else { + normalizedProps[i] = props[i]; + } } } diff --git a/jsx-runtime/test/browser/jsx-runtime.test.js b/jsx-runtime/test/browser/jsx-runtime.test.js index b43b08ec71..ac678ce477 100644 --- a/jsx-runtime/test/browser/jsx-runtime.test.js +++ b/jsx-runtime/test/browser/jsx-runtime.test.js @@ -34,8 +34,16 @@ describe('Babel jsx/jsxDEV', () => { it('should keep ref in props', () => { const ref = () => null; - const vnode = jsx('div', { ref }); + const props = { ref }; + const vnode = jsx('div', props); expect(vnode.ref).to.equal(ref); + expect(vnode.props).to.not.equal(props); + }); + + it('should not copy props wen there is no ref in props', () => { + const props = { x: 'y' }; + const vnode = jsx('div', props); + expect(vnode.props).to.equal(props); }); it('should add keys', () => {