Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance and reduce memory allocation #4521

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions compat/src/forwardRef.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { options } from 'preact';
import { assign } from './util';

let oldDiffHook = options._diff;
options._diff = vnode => {
Expand All @@ -25,9 +24,13 @@ export const REACT_FORWARD_SYMBOL =
*/
export function forwardRef(fn) {
function Forwarded(props) {
let clone = assign({}, props);
delete clone.ref;
return fn(clone, props.ref || null);
if (!('ref' in props)) return fn(props, null);

let ref = props.ref;
delete props.ref;
const result = fn(props, ref);
props.ref = ref;
return result;
}

// mobx-react checks for this being present
Expand Down
12 changes: 3 additions & 9 deletions jsx-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,9 @@ function createVNode(type, props, key, isStaticChildren, __source, __self) {
ref,
i;

if ('ref' in normalizedProps) {
normalizedProps = {};
for (i in props) {
if (i == 'ref') {
ref = props[i];
} else {
normalizedProps[i] = props[i];
}
}
if ('ref' in props) {
ref = props.ref;
delete props.ref;
}

/** @type {VNode & { __source: any; __self: any }} */
Expand Down
3 changes: 2 additions & 1 deletion jsx-runtime/test/browser/jsx-runtime.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ describe('Babel jsx/jsxDEV', () => {
const props = { ref };
const vnode = jsx('div', props);
expect(vnode.ref).to.equal(ref);
expect(vnode.props).to.not.equal(props);
expect(vnode.props).to.equal(props);
expect(vnode.props.ref).to.equal(undefined);
});

it('should not copy props wen there is no ref in props', () => {
Expand Down